Possible to set the ID of a new bean?

74 views
Skip to first unread message

abxccd

unread,
Apr 1, 2012, 1:08:30 AM4/1/12
to redbe...@googlegroups.com
I need to create a record with a certain ID (the ID is guranteed to not be in used by another existing row.
 
I have tried:
$bean = R::dispense('mytable);
$bean-> id = 20;
$bean->property = "some property";
R::store($bean);
 
The problem is that the bean never gets saved. Is this something that we can do in redbean?

gabor

unread,
Apr 1, 2012, 4:16:25 PM4/1/12
to redbeanphp
Unfortunately this is not possible because RedBeanPHP uses
autoincrement IDs.
If the id > 0 RedBeanPHP thinks the bean has already been inserted and
it will try to update the record.
I recommend to use another id-field for this, I guess this is for
external system interaction?

abxccd

unread,
Apr 1, 2012, 10:36:59 PM4/1/12
to redbe...@googlegroups.com
Hi Gabor,
 
Thanks for your help :)
 
Actually, it's just to make sure that a certain record has a certain ID in a permission authorization system. Because I am inserting some default permissions, where the ordering is important.
 
But that's alright, because I have written my script to insert data in order. The setting the ID was just to try and make it foolproof, incase the insertion order were changed. :)

Anton A.

unread,
May 3, 2015, 6:17:26 PM5/3/15
to redbe...@googlegroups.com
What is the sense to have 2 IDs if the external ID is just a BIGINT and perfectly fits as a default PK that RedBean would understand?
RedBean concept and approach is so nice, but this simple issue makes it really non-applicable to many cases.

Jake Chapa

unread,
May 3, 2015, 6:24:28 PM5/3/15
to redbe...@googlegroups.com
R::exec(‘UPDATE table SET id=NEW_ID where id=OLD_ID’);

--
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/d/optout.

Anton Andriyevskyy

unread,
May 4, 2015, 8:15:58 PM5/4/15
to redbe...@googlegroups.com
It is a workaround and has its own drawbacks and limitations:
1. 1+ query - not good for daily inputs of million records with long transaction bodies.
2. ID collisions
3. ID still needs to be autoincrement
You received this message because you are subscribed to a topic in the Google Groups "redbeanphp" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/redbeanorm/tQk_eY1K3pQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to redbeanorm+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages