What is the bug:

When a Doctrine_Record is saved (using save()) _id is not fully updated. 

This problem is occurring for me using MySQL, with tables that have 
composite primary keys (and columns that need it do have auto_increment 
set). The schema was auto-generated from the database, and the model from 
that schema. I am using a fresh install of symfony 1.4 (as of last week).

Debugging this showed that _assignIdentifier(Doctrine_Record $record) 
in*..../Doctrine/Connection/UnitOfWork.php
* always ultimately called $record->assignIdentifier(true) and never 
therefore retrieved the last insert id. 

Further debugging in *..../Doctrine/Record.php* shows that:

1) public function assignIdentifier($id = false) will always call 
$this->prepareIdentifiers(true)
2) private function prepareIdentifiers($exists = true) will for type 
Doctrine_Core::IDENTIFIER_COMPOSITE 
merely copy into $this->_id any currently valid data from the table - and 
hence will always miss the auto increment column.

I do have a 'dirty fix' - which will is safe for my use of symfony (as the 
auto_increment column is always first in my tables with composite keys). The 
fix is to:

1) Get the last sql insert id before calling $record->assignIdentifier(true)
2) In the case when a composite key is being processed - if there is no data 
for the first primary key, set it to the last sql insert id if it exists (as 
now passed in via assignIdentifier and prepareIdentifiers).

This works fine - and I can now do:

$foo->save();
$foo = FooTable::getInstance()->find($foo->identifier());

The changes (CVS diff against symfony 1.4) are:

===================================================================
RCS file: 
vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php,v
retrieving revision 1.1
diff -r1.1 Record.php
760a761
>      * @param mixed $id - $id to use if none found in a composite 
identifier
763c764
<     private function prepareIdentifiers($exists = true)
---
>     private function prepareIdentifiers($exists = true, $id = -1)
781a783
>                 $count = 0;
784a787,790
>                         if($count == 0 && $id != -1)
>                         {
>                             $this->_id[$name] = $id;
>                         }
787a794
>                     $count++;
2228a2236
>      * @param mixed $tempId a key value from SQL last insert ID (for use 
in composite key ID assignment)
2231c2239
<     public function assignIdentifier($id = false)
---
>     public function assignIdentifier($id = false, $newId = -1)
2239c2247
<             $this->prepareIdentifiers(true);
---
>             $this->prepareIdentifiers(true, $newId);

Index: 
vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php
===================================================================
RCS file: 
vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php,v
retrieving revision 1.1
diff -r1.1 UnitOfWork.php
936d935
< 
957c956,957
<             $record->assignIdentifier(true);
---
>             $id = $this->conn->sequence->lastInsertId();
>             $record->assignIdentifier(true, $id);


I am not sure if this is a schema or model generation bug or a doctrine bug 
and I am new to symfony (as of last week) so there may be a better 
workaround or fix, however the fix above was the quickest way for me to 
solve them problem.

Any thoughts welcome.

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to