gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

Cannot cast to bean with similar fields and underscore #705

Closed delafred closed 5 years ago

delafred commented 5 years ago
$ta = R::load('utilisateur', 1);
$ta->langue_id = 4;  // int(6)
$ta->langue = "fr"; //varchar(2) utf8 general CI
R::store($ta);

Fatal error: Uncaught RedBeanPHP\RedException: Cannot cast to bean. in /home/website/public_html/fonctions/rb.php:2436 Stack trace: #0 /home/website/public_html/admin/test.php(11): RedBeanPHP\OODBBean->__set('langue', 'fr') #1 {main} thrown in /home/projet/public_html/fonctions/rb.php on line 2436

It seem that redbean missread informations between "langue_id" and "langue"

If I quote line with "$ta->langue" it work if I quote line with "$ta->langue_id" it still not work

Reproduced on RB 5.0 & 5.3 MySQL 5.6 // PHP 7.2

Any idea ?

Lynesth commented 5 years ago

Hey there @delafred

It seems there's a bit of misconception here.

I guess you have a table named langue which contains at least 2 columns, id and something else (in which you have 'fr', 'en', and so on). If everything is correct, your table utilisateur contains a column langue_id but no column langue.

Now, when you set that language for a user you are actually linking the langue bean to the utilisateur bean, and there's 2 way of doing this. Either you use the id of the langue:

$ta = R::load( 'utilisateur', 1 );
$ta->langue_id = 4;
R::store( $ta );

Or you use the langue bean:

$ta = R::load( 'utilisateur', 1 );
$fr = R::load( 'langue', 4 );
$ta->langue = $fr;
R::store( $ta );

In the second case, Redbean will extract the id from that bean and set it to the column langue_id of your utilisateur table.

Let me now if that wasn't clear and I will explain myself in french for you.

Lynesth commented 5 years ago

TL;DR You cannot have a column named xxx and a column named xxx_id on the same table since it points to the exact same thing for RedBean: the xxx bean.

delafred commented 5 years ago

Thanks for your answer, will try to continue in English for the community

You cannot have a column named xxx and a column named xxx_id on the same table since it points to the exact same thing for RedBean: the xxx bean.

This is the unpredicted behaviour that I have discovered and not a model conception error (even if it can be strange from outside to see those 2 fields on the same table). Is it a convention like "restricted naming" or is it a bug ?

I fully undestand the need of convention and it's one of the powerfull of this ORM. Is there a way I can help to define and put on website "actual conventions" as I didn't find any uptodate doc on this ?

https://redbeanphp.com/manual3_0/index.php?p=/manual3_0/schema is obsolete https://www.redbeanphp.com/index.php?p=/crud seem incomplete

Tables naming:

Fields naming:

Lynesth commented 5 years ago

I guess that's the most you can find: https://www.redbeanphp.com/index.php?p=/many_to_one

I agree that a list of Redbean's convention in a page (named ie. "Conventions") would be a nice addition @gabordemooij

gabordemooij commented 5 years ago

Okay, I will add it to https://www.redbeanphp.com/index.php?p=/crud and turn it into a list. Thanks for reporting!

gabordemooij commented 5 years ago

Since this is a shortcoming in the manual this will count as a bug. I tagged this issue as a bug.

gabordemooij commented 5 years ago

https://www.redbeanphp.com/crud#conventions

Lynesth commented 5 years ago

@gabordemooij I feel like this is ok for now. Maybe we can talk about it again when the manual is available on github for edition ? ;)

gabordemooij commented 5 years ago

yes, I have made steps towards that