specify custom id (PRIMARY KEY) when saving new/dispensed bean (dispense, set id and save)

428 views
Skip to first unread message

Tomáš Klapka

unread,
Feb 6, 2012, 6:56:09 PM2/6/12
to redbeanphp
Hello,

I need to migrate some already existing data with already existing ids
as primary key for every record but it is not incremental. There are
missing some ids in the sequence because some records were deleted
already and it does not even start at 1. There are some other parts of
application used to refer to these ids, so I cannot populate new ids
for the data.

I would like to migrate it by RedBean but when I set id before save
($record->id = 10; $record->save();) it does UPDATE my_table SET ...
WHERE id = 10; eventhough record with id=10 does not exist yet

Is there any way how I can specify the id value when creating or
saving the record? Or even changing the id value when the record
already exists.

I know I can create the table by SQL and just import the migrated data
as CSV, though I would like avoid SQL and let RB to dynamically create
the table for me.

I can create old_id or legacy_id so I can keep the old ids, but I
don't see a reason to generate new ids for the old data.

Only thing which comes to my mind is that I could maybe create some
fake record, delete it (so it creates the schema) and then import the
CSV by calling INSERT SQL for every row.

Maybe it is a bad practice to rely on old ids. I guess it is not good
for other app parts to refer ids. What do you think?

Best regards,
Tomáš Klapka

Jake Chapa

unread,
Feb 6, 2012, 7:03:36 PM2/6/12
to redbe...@googlegroups.com
make another field

$record->real_id = 10;

Then, do everything normally. When you are finished, delete the 'id' column and rename 'real_id' to 'id'. Dont' forget to make the column a auto-increment primary key also.

> --
> You received this message because you are subscribed to the Google Groups "redbeanphp" group.
> To post to this group, send email to redbe...@googlegroups.com.
> To unsubscribe from this group, send email to redbeanorm+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redbeanorm?hl=en.
>

indiekiduk

unread,
Nov 13, 2013, 2:33:10 PM11/13/13
to redbe...@googlegroups.com
You could also just do a regular sql query like:
R::exec('insert into protect(id) values(?)',array($id)) 
Then your id would be there and when you store the bean it will update that row. However if you have a lot of data it isn't efficient to do 2 queries per row. So you could just insert all fields in the exec, and just use beans for your queries, however then you lose RedBean's magic of inserting or finding any related beans. With a few modifications you could use a special meta field to force an insert rather than an update when (!$id or getMeta('forceInsert')), and then pass the $id to the insertRecord() and if its not null use that instead of the $default.  I investigated this because the table I was migrating to redbean had 3 relations, however in the end I just reverted back to using exec for all fields and I just find() and store() each of the related beans myself first before using their ids in the exec(). Since store() checks if tainted, it doesn't hit the db if the bean isn't a new one. And I assume somewhere in red bean it does the same thing for when you insert a bean with a related bean. So overall performance is probably the same.
Reply all
Reply to author
Forward
0 new messages