RE: [PEAR] DB class, MySQL and nextId()
| From: | Torsten Roehr | Date: | Thu, 15 Apr 2004 21:03:24 +0000 |
| Subject: | RE: [PEAR] DB class, MySQL and nextId() | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-11885@lists.php.net to get a copy of this message | ||
> I'm curious what the point is of nextId() in the mysql driver class for
> DB? Unless I'm (*really*) missing something, it's implementation is
> utterly worthless for INSERT queries into tables that already have
> data. What nextId() returns is the last_insert_id() for the sequence
> you create not the last_insert_id() for the table that you are actually
> inserting data to. So what's the point?
Not every DB has something like last_insert_id(). Using sequences is more
DB-independent. This means you get the id BEFORE the first 'real' insert
which makes data handling easier and you can safely use this value for
relating inserts in other tables (foreign key inserts).
> And how are you supposed to get the last_insert_id() for the table
> you are inserting data to using the class? Or are you supposed to do
> that on your own?
You call nextId() before an insert and use this for your next insert. You
don't need last_insert_id because you already have the id from nextId().
Example:
// open DB connection
// ...
$newID = $db->nextId('table');
$myObject =& new Classname()
$myObject->setID($newID);
// set other object attributes
$query = "INSERT INTO table SET id = " . $myObject->getID() . ", column1 =
'" . $myObject->getName() . "'";
// commit query
Hope this helps,
Torsten Roehr
>
> thnx,
> Chris
>
> --
> PEAR General Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>