Home » Php » php – Does RedBean need a "id" primary key?

php – Does RedBean need a "id" primary key?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

The accepted answer is not strictly true… You can use the existing table structure – but you would need to implement a VIEW on top of each of the tables that allow you to rename the PKEY column to be ‘id’… See this email from Gabor – the creator of RedBeanPHP:

https://groups.google.com/forum/#!searchin/redbeanorm/view/redbeanorm/wXUeT4Tj2uU/3AngnmVwZdYJ