views:

28

answers:

1

I'm pretty new to Doctrine, but as I understand it, the assignIdentifier() method is supposed to tell Doctrine to update the relevant row into the database instead of inserting a new one.

I have an object that I'm building through a workflow, so the identifier has an id of null until I call $object->save(); which inserts it, and this does work.

If however I call $object->assignIdentifier($newobj->id); and then $object->save(); it does nothing - it does not insert a new row and does not update the old one.

If a certain condition is true, I want to pull a different record out of the DB and update that row instead of inserting the new one.

Am I understanding something wrong here?

Some code to illustrate:

if($this->object->payments > 0) {           
    $older_payment = Doctrine_Query::create()
       ->from('OldPaid p')
       ->where('p.dealid = ?', $this->object->transid)
       ->fetchOne()
    ;

    $this->object->assignIdentifier($older_payment->id);
}

$this->object->save();
A: 

Usually, if you retrieve a record, you can update it with the save() method. Doctrine recognizes this (since the PK doesn't change) and updates the record.

From the docs:

Updating objects is very easy, you just call the Doctrine_Record::save() method

Another way can be replace(), but I usually use just save() and does either the saving or the updating if the record already exists.

As far as I can read from the description of assignIdentifier() never used it myself) it will only work with retrieving an object by its ID, so updating something with this method will not work.

DrColossos