I'm using RedBean PHP to dump some data from a web scrape into a database, and I need to retain the legacy IDs, preferably as the primary key field. Is it possible to do this with RedBean?
When I try to set the id as so:
$bean->id = 56;
The row doesn't get inserted - the query that ends up being created instead becomes an "UPDATE WHERE id
=56", which does nothing since the record doesn't exist yet.
I solved this by inserting a placeholder with an SQL exec first:
$id = 60000;
$name = 'bob';
R::exec('insert into person(id) values('.$id.')');
$person = R::dispense('person');
$person->id = $id;
$person->name = $name;
R::store($person);
Forcing a specific value on an auto-increment primary index is just asking for race-condition problems. I'd suggest you create a new column legacy_id
instead where you save the alternative id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With