RedBean需要一个“id”首要的关键? [英] Does RedBean need a "id" primary key?

查看:519
本文介绍了RedBean需要一个“id”首要的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用 RedBean ORM,是否需要为数据库中的每个表添加一个名为id的主键?



在我的db我有几个表有主键对2或3字段,或主键与其他名称不是id(是的,我可以更改名称id,但它不会真实反映现实,因为它们不是ID)



示例:
$ b

table1 - 存储帖子:

  id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT ,
title TEXT,
content TEXT,

table2 - :

  post INTEGER DEFAULT 0,#< -  referencesidfromposts
name TEXT,
value TEXT,
PRIMARY KEY(name,post),
CONSTRAINT posts_meta FOREIGN KEY(post)
参考文章(id)ON删除CASCADE ON UPDATE RESTRICT

RedBean是否可以使用这种数据库结构?

方案

不幸的是,对于当前的表结构,你不能使用RedBean。每个表都需要有一个自动增量主键。



由于这个约束,无法使用RedBean的几个线程,以及作者的回应,Gabor De Mooij:



http://groups.google.com/group/redbeanorm/browse_thread/thread/6d5582275326744f?pli=1



http://groups.google.com/group/redbeanorm/browse_thread/thread/4fa7b29b453dcdb8



但是RedBean并不要求主键字段被简单地命名为id。您可以使用 formatBeanID()方法来格式化主键列的名称,如下面的示例所示,它将表名前缀为id 有条件的。例如)table users 将使主键为 users_id 。使用此格式,您可以根据需要使用ID名称进行详细说明。



http://redbeanphp.com/community/wiki/index.php/Prefixes



希望这个限制在将来解除



编辑:作为另一种ORM,我听说过Doctrine: a href =http://www.doctrine-project.org/> http://www.doctrine-project.org/ 。我没有亲自使用它,但它似乎是许多使用PHP的标准。



编辑2:感谢和信用Jason引起注意一种新的方法,将RedBean集成到现有项目中,其中可能没有为其设置数据库。我想更新我的答案以及如果人们仍然引用这个问题。 Gabor建议创建映射到表的视图,您可以在其中设置视图以具有RedBean所需的正确结构。我没有亲自测试这个,但它得到了一些用户的积极反馈。它在更改表时增加了一些额外的开销和维护,但似乎是迄今为止这个问题的最好和最完整的答案。
http://www.redbeanphp.com/faq#beanformatter


If you use RedBean ORM, do you need to add a primary key named "id" to every table in your database?

In my db I have a few tables that have primary keys pairs of 2 or 3 fields, or primary keys with other names than "id" (yes, I could change the name to "id", but it wouldn't really reflect the reality, because they are not IDs)

Example:

table1 - stores posts:

  id           INTEGER      PRIMARY KEY AUTOINCREMENT,
  name         TEXT,
  title        TEXT,
  content      TEXT,

table2 - stores meta for posts:

  post         INTEGER      DEFAULT 0,     # <- references "id" from "posts"
  name         TEXT,
  value        TEXT,
  PRIMARY KEY(name, post),
  CONSTRAINT posts_meta FOREIGN KEY(post)
    REFERENCES posts(id) ON DELETE CASCADE ON UPDATE RESTRICT

Would RedBean work with this kind of db structure?

解决方案

Unfortunately, with how your current table structure is, you couldn't use RedBean. Every table needs to have an auto-increment primary key. A slight downfall, as it makes integration into an already existing product more difficult.

A couple of threads that failed to use RedBean due to this constraint, with responses from the author, Gabor De Mooij:

http://groups.google.com/group/redbeanorm/browse_thread/thread/6d5582275326744f?pli=1

http://groups.google.com/group/redbeanorm/browse_thread/thread/4fa7b29b453dcdb8

RedBean does NOT require the primary key field to be named simply "id", however. You can format the name of the primary key column to your liking by using the formatBeanID() method, as seen in the example below, which prefixes the table name to the "id" conditionally. eg) table users would have the primary key be users_id. Using that formatting, you can get as detailed with the id name as needed.

http://redbeanphp.com/community/wiki/index.php/Prefixes

Hopefully this restraint will be lifted in the future, since it really does hamper the integration into existing products.

EDIT: As an alternative ORM, I've heard well of Doctrine: http://www.doctrine-project.org/. I haven't personally used it, but it seems to be the standard for many working with PHP.

EDIT 2: Thanks and credit to Jason for bringing to attention a new method for integrating RedBean into an existing project where your database might not be set up for it. I wanted to update my answer as well in case people still reference it with this problem. Gabor suggested making views that map to the tables, where you can set up the view to have the proper structure required for RedBean. I have not personally tested this, but it has gotten positive feedback from some users. It adds some extra overhead and maintenance when altering tables, but seems to be the best and most complete answer to this issue to date. http://www.redbeanphp.com/faq#beanformatter

这篇关于RedBean需要一个“id”首要的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆