Beans name with underscore

980 views
Skip to first unread message

Iván Carreón

unread,
Oct 14, 2013, 2:57:08 PM10/14/13
to redbe...@googlegroups.com
Hello all,

Any suggestions to use bean names with underscore?

Thanks!

Ajay Bramhe

unread,
Nov 6, 2013, 8:05:55 AM11/6/13
to redbe...@googlegroups.com
in naming rules it is written that underscore is used to show relation between tables :)
please correct me if i am wrong...
(i am a beginner in redbeanphp orm)

Chris Smith

unread,
Nov 26, 2013, 10:41:57 AM11/26/13
to redbe...@googlegroups.com

gabor

unread,
Nov 27, 2013, 4:10:27 PM11/27/13
to redbe...@googlegroups.com

Hi,

RedBeanPHP does enforce certain schema policies.
This keeps it possible to do all the work without either caching foreign keys or dynamically querying the schema.
An underscore in a type denotes a relation between two types. Although it is technically possible to introduce such
beans you might experience unexpected results.

cheers,
Gabor

Anže Časar

unread,
Nov 30, 2013, 12:22:26 PM11/30/13
to redbe...@googlegroups.com
Hi,

I actually use RedBean with underscores in bean names. I think that "unlocking" that feature actually makes RedBean more powerful, because it gives you more possibilities.  I think that in many cases you simly can't avoid naming your beans with multiple words (e.g. product, product_category, blog_item, blog_category, ...) and underscores make them much more readable. And you can even dispense "many-to-many tables" as beans and add attributes of your choice to the connection.

Right now, to use underscores you just need to modify the Facade a little bit (change the regular expression in the dispense() function from '/^[a-z0-9]+$/' to '/^[a-z0-9_]+$/'). Most of the RedBean features will still work and you can still use stuff like $user->ownBlogItem = $item , so it all works. But you need to be careful not to create any conflicts in the database schema. Plus you will need to live with weird key names that are generated by export functions (like sharedProduct_category).

I actually think it would be possible to allow users to configure schema policies a little (maybe an idea for RB 4.0 ;)). For example, if we allow users to enable underscores in bean names, we can change the char linking the two tables from "_" to "__" (or even "_mtm_") and that way avoid all conflicts.

Btw, tnx Gabor for your great work and making our lives easier :)

Cheers,
Anze

David Deutsch

unread,
Dec 1, 2013, 5:54:47 AM12/1/13
to redbe...@googlegroups.com
I think it's about information architecture. From your examples:

"product_category" would be a linking table, so well within the scope of relations in RB.

"Blog" isn't really a datum, it's a concept. What you have in a blog are articles. So you could have an "articles" and a "articles_category" as a linking table.

In most cases, going through the trouble of /avoiding/ underscores actually ends up making your data structure a lot clearer and free from redundancies. So I think it is a constraint that should stay because even though it is a constraint, it's a helpful one.

-David

Anže Časar

unread,
Dec 1, 2013, 7:22:29 AM12/1/13
to redbe...@googlegroups.com
Maybe my example wasn't the best one... The point I was trying to make was that you can't have the same attributes in product categories and article categories. So following your pattern, you would need to create two different tables called category? And there are some better examples too.

You often want to organize your code in plugins (or components or bundles) as it makes your code more readable and re-usable. And each plugin needs to have its own unique tables. So you have a  "product" plugin with tables like `product`, `category`, `filter`, ... and then a "blog" plugin with `article`, `category`, `filter`, ... And you have a problem. :) To think of unique table names in a project with 20+ plugins can become a challenge of its own. And an easy solution is to prefix your tables with plugin name (so, `blog_article`, `blog_category`, ...)

Another example: in multilingual tables you need to store some data in different languages. One of the best ways to do that is to create translation tables for every entity. For example: `product` (id, price, image) and `product_i18n` (id, product_id, language_id, title, description). Without underscores, things become ugly (`producti18n`).

To sum up, in small scale projects I agree with you completely. But in medium and large scale projects with 100+ tables in the database, this little constraint can quickly become a big one.

Regards, Anze

gabor

unread,
Dec 9, 2013, 3:14:53 PM12/9/13
to redbe...@googlegroups.com


I think 'unlocking' this feature will take away the charm and simplicity of RedBeanPHP.

* There is no way to distringuish N-M relations from prefixed tables
* How should RedBeanPHP know what module a table belongs to when generating it ?
* Should we support custom table mappings as well, what about page_suffix or prefixpage ?

Then RedBeanPHP will no longer be configurationless.

I don't think this has anything to do with scale. I have worked on pretty big projects myself (hospitals).
It has to do with mindset. Trying to keep things simple.

Plugin architectures can pose a problem if you have many contributors and they don't choose their type names carefully.
But I don't think adding prefixes is a solution for that. Our job is to manage complexity, not to facilitate it.
Software evolves, our job is to guide its evolution. We can make it complex, we can keep it simple.

cheers,
Gabor

Marcio Almada

unread,
Dec 10, 2013, 8:14:45 AM12/10/13
to redbe...@googlegroups.com
And what about about Anže Časar idea of using a different identifier to indicate linking tables?

I actually think it would be possible to allow users to configure schema policies a little (maybe an idea for RB 4.0 ;)). For example, if we allow users to enable underscores in bean names, we can change the char linking the two tables from "_" to "__" (or even "_mtm_") and that way avoid all conflicts.

I think the idea of using "__" for linking is quite reasonable. No matter what philosophy redbean is based on, being not able to choose a table name with underscores can be a huge let down in many situations. All other conventions are rationally awesome, but this one always sounded as too much enforcement for most people.

Marcio Almada

unread,
Dec 10, 2013, 8:21:42 AM12/10/13
to redbe...@googlegroups.com
Humm, double underlines "__" seems to be a nice idea

gabor

unread,
Dec 11, 2013, 4:27:15 PM12/11/13
to redbe...@googlegroups.com

I feel we are on the wrong track here.
RedBeanPHP needs to focus on simplicity.

We should not try to turn it into an 'enterprise' library or ORM 'framework'.
Limitations are good. Every tool has its use. If you need something stronger, go download Doctrine.

cheers,
Gabor

Marcio Almada

unread,
Dec 16, 2013, 9:14:46 AM12/16/13
to redbe...@googlegroups.com
But why not double underlines? So everyone would be finally able to use underscored table names. I think this is the most intrusive point of redbean conventions and the most popular feature request of all times, would be so nice for redbean 4.

Underscored table names are not that evil to become an anti pattern, bad table naming is but no ORM should enforce that IMMO.

Jake Chapa

unread,
Dec 16, 2013, 9:20:13 AM12/16/13
to redbe...@googlegroups.com
No offense to gabor, but the first thing I do with rb is edit it to allow for underscore. It's a simple regex.

--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To post to this group, send email to redbe...@googlegroups.com.
Visit this group at http://groups.google.com/group/redbeanorm.
For more options, visit https://groups.google.com/groups/opt_out.

Jérémie Robert

unread,
Feb 26, 2014, 11:41:18 AM2/26/14
to redbe...@googlegroups.com
redbeanphp is a great tool ! I like it

But i think the double underlines is a good idea too.

With mysql, the underscore in the table name are used a lot, for prefix, relation...

Another point, in the documentation, you can see :

 $b R::dispense'2beans' ); //invalid

In fact that's work with the last version, without modification.

Thanks, keep it up

gabor

unread,
Feb 26, 2014, 4:14:35 PM2/26/14
to redbe...@googlegroups.com


Hi,

the error in the documentation has been fixed, thanks for reporting.

cheers,
Gabor

Lionel Marbot

unread,
Aug 13, 2014, 9:30:24 AM8/13/14
to redbe...@googlegroups.com

We would also very much appreciate to be able to have underscores in beans. So far, this incapability highly diminishes the areas in which we can use your software, and that is very sad.

Thank you for your reconsideration!

Andrew Walker

unread,
Sep 1, 2014, 6:02:07 PM9/1/14
to redbe...@googlegroups.com
@gabor I agree that RedBeanPHP should be all about simplicity. This is where it excels and what sets it apart from other ORMs. I however don't think a schema change is in contrast to this philosophy.

The fact is, developers normally name their tables with underscores. Wouldn't the simplest path be to follow what is considered the norm?

It's a small change but it goes a long way:
  • It simplifies readability. e.g. `producti18n` vs `product_i18n`
  • It simplifies adoption. Less need to create VIEWs to force tables into the RedBeanPHP schema.
  • It simplifies interoperability with existing schema. e.g. R::dispense('wp_posts');
  • It simplifies the documentation. You won't need to tell people to avoid using underscores
Using "__" instead of "_" to denote m2m seems like a perfectly viable solution to the problem.

gabor

unread,
Sep 2, 2014, 2:24:18 PM9/2/14
to redbe...@googlegroups.com

No.
Reply all
Reply to author
Forward
0 new messages