gabordemooij / redbean

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

bean store issue after duplicate #917

Closed s4dnss closed 1 year ago

s4dnss commented 1 year ago

Hi! I'm trying to duplicate a bean. The duplicated bean has all the values, but when I store it, in the database all the values will be NULL, except id and values that I've changed... When I check again the bean, it still has all the values and get the new id as well. But in the database, there's nothing...

What have I missed?

$bean = R::load('book',1); $clone = R::duplicate('book'); var_dump($clone); // looks everything fine, id = 0 R::store($clone); // in database all fields are NULL except id var_dump($clone); // everything fine, get id = 2

gabordemooij commented 1 year ago

Just checked, this works fine:

$bean = R::load('book',1);
$bean->title = 'Title';
$clone = R::duplicate($bean);
R::store($clone);
echo " Title = {$clone->title}\n";

Output:

Title = Title

Or can you provide a simple, compact example that demonstrates the issue?

s4dnss commented 1 year ago

I've made some tests, and I figured out what could be the problem. I turned on fancydebug, and search for differences between working and nonworking cases. Everything works fine, until I've made an UPDATE on another table before duplicate. If I do so, RedBean will store NULL values. Only modified values will be stored.

I can solve my problem from that point, but I thought you should know that.

Thanks!

gabordemooij commented 1 year ago

Can you give a tiny code example please?

gabordemooij commented 1 year ago
$other = R::dispense('other');
$other->test = 1;
R::store($other);
$bean = R::load('book',1);
$bean->title = 'Title';
$clone = R::duplicate($bean);
$other->test = 2; //update prop
R::store($other); //update store
$other->test = 3; //update not saved
R::exec('UPDATE other SET test = 4'); //update sql
R::store($clone);
echo " Title = {$clone->title}\n";
//still works?
s4dnss commented 1 year ago

Okay, it's my mistake. The UPDATE in my code, that is before duplicate use the usePartialBeans(true) function, and I didn't turned it off. Honestly I don't remember why I use it there, and I have to check what is does, but that would be the problem. Sorry to bother you.

gabordemooij commented 1 year ago

Ok, no problem, always happy to help.