Question

The question is about doctrine 1.2 and Symfony 1.4

In short: I am running simple SQL query and fetching results into array with PDO. After it, I create Doctrine_Collection of the same table I've fetched data from and call to synchronizeFromArray to load the data into collection.

Everything is fine - collection is created and all the data up there beside of my primary key which happens to be boolean false instead of real value.

Here is example of the code:

// Fetch single object from DB
$sql = "SELECT * FROM payments LIMIT 1";
$p = $connection->query($sql)->fetchAll(PDO::FETCH_ASSOC);

var_dump($p); // I see that all the data including `id` is ok

$c = new Doctrine_Collection('Payment', 'id');
$c->synchronizeFromArray($p);

var_dump($c->toArray()); // All the data is ok but `id` == false

I tried to use Doctrine_Collection::fromArray instead of synchronizeFromArray but it give same result

Was it helpful?

Solution

  1. During Doctrine_Collection::fromArray (synchronizeFromArray too) record primary keys (DoctrineRecord::_id array) won't get updated.

  2. DoctrineRecord::toArray() will overwrite all primary key column values with result of DoctrineRecord::getIncremented() which because of point 1. is empty.

One way to deal with this is to modify your $p array before you perform fromArray(). Inside your array you should change your primary key name to _identifier.

In your case simplest solution would be to modify your SQL query:

$sql = "SELECT `id` as `_identifier`, * FROM payments LIMIT 1";

This way following code from Doctrine_Record::fromArray():

if ($key == '_identifier') {
    $refresh = true;
    $this->assignIdentifier($value);
    continue;
}

will assign proper _id array to your records.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top