Search code examples
phpredbeantablename

Why is RedBean not using plural table names?


Or can we do that? It's doesn't look good too when you say:

$book = R::dispense('books');

and just handle the naming with an RedBean_IModelFormatter or am I missing something? Thanks.


Solution

  • You can do it, but it gets confusing and doesn't look good. The standard convention is non-plural table names to make more sense when dispensing a book and storing a book, as well as with relation mapping. Imagine if you will the following:

    $book=R::dispense('books');
    $book->title='War and Peace';
    $pageOne=R::dispense('pages');
    $pageOne->number=1;
    $pageTwo=R::dispense('pages');
    $pageTwo->number=2;
    $book->ownPages=array($pageOne,$pageTwo);
    R::store($book);
    

    That looks fine. Now lets say we load a page and go to fetch the associated book:

    $page=R::load('pages',1);
    echo $page->books->title;
    

    This doesn't make as much sense and doesn't quite fit what RedBean is after. Again, you can do it. If you have a database already set up, you may not want to make those changes. Again, it is all your decision and what you are trying to accomplish. If it is just you, then do whatever. If you are working in a group, it might make more sense to rename the tables so that your work is more legible.