Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to manually set ID of new rows with RedBean PHP?

Tags:

php

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.

like image 622
Jordan Avatar asked Dec 12 '22 15:12

Jordan


2 Answers

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);
like image 103
malhal Avatar answered Dec 29 '22 06:12

malhal


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.

like image 44
deceze Avatar answered Dec 29 '22 05:12

deceze