Subject:
DataObjects Insert Problem
From:
Leigh Caplan <maelman@u.washington.edu>
Date:
Sat, 16 Nov 2002 03:24:19 -0800
To:
pear-general@lists.php.net
Hi there... I'm having a bit of a problem using the insert() method on some of my objects. I'm manually setting the primary key, because it's actually two rows- both "user_id" and "order_id".
mmh. - if you have manually altered the database.ini file to have two lines for
[table__keys]
I'm not sure what the results would be...
So, I want the "user_id" field to stay consistent, but the "order_id" field should increment based on the contents of the "user_id" field. That way, orders will be numbered according to user, ie. user_id=1, order_id=1; user_id=1, order_id=2...user_id=2, order_id=1... etc...
it sounds like overrideing the insert method is probably the trick here..
function insert() {
// do stuff to build order id and user id...
parent::insert();
}
Everything seems fine until I call the insert() method on my object... after this, its fields which are listed in the database.ini file as keys are reset to "0".
Im not 100% sure why, but they keys only currently supports single autoincrement type keys.. so adding more than one key would have unexpected results..
I tried the same method on another type of object which uses an auto_increment field as its primary key, and the key was returned properly upon insert.
if you are using mysql, it uses mysql_insert_id(), to make the return value for insert() (and sets the key column to match it..), otherwise it just uses the nextid() stuff from pear..
It should be noted that the data is properly inserted into the database, but the object variables corresponding to the primary key(s) are reset (I assume they're being passed NULL keys which are then cast to integers, the end result being variables assigned a value of zero).
I'm guessing it's the issue above.. - if you are setting the key manually, either remove it from the database Keys file.. (in which case static get may be less usefull)..
or try overriding the insert() method
function insert() {
// do stuff to build order id and user id...
$id = 'some calculated id';
$this->specialid = $id;
parent::insert();
$this->specialid = $id; // reset id as it's
}
What could be causing this? I suppose I could keep copies of all my explicitly assigned keys and just put them into the object after it's inserted, but this would be a major kludge, and would defeat the purpose of simplifying my code (which is why I'm using dataobjects in the first place).
Thanks in advance,
Leigh Caplan
maelman@u.washington.edu
Regards
Alan