Question
I’m using Symfony1.4 and Doctrine1.2 to create children objects and print them in execution time. The problem is that I always get the children objects from the first query.
I have the following schema:
1 2 3 4 5 6 7 8 | Parent: id name Child: id parent_id name |
My operations:
- Initially I have one Parent (with id=1), and no Child
- I grab a Parent (id=1) and store that object on $parent
- List it’s Child objects
- Result: none OK: as expected
- Create a new Child and set it’s parent to 1
- List $parent‘s Child objects
- Result: none OOPS: I expected the new Child from 5.!
Code:
1 2 3 4 5 6 | $parent = Doctrine_Query::create()->from('Parent p')->where('p.id = ?', 1)->limit(1)->fetchOne(); /* from 2. */ print_r($parent->getChild()->toArray()); /* from 3. */ $child = new Child(); $child->setParentId($parent->getId()); $child->save(); /* from 4. */ print_r($parent->getChild()->toArray()); /* from 6. */ |
Note, from Doctrine’s comments:
1 2 3 4 5 6 7 8 9 10 11 | /** * refresh * refresh internal data from the database * * @param bool $deep If true, fetch also current relations. Caution: this deletes * any aggregated values you may have queried beforee * * @throws Doctrine_Record_Exception When the refresh operation fails (when the database row * this record represents does not exist anymore) * @return boolean */ |
I’ve already tried re-grabbing the $parent before the last line, but the result is the same.
Own Answer
I found a work-around for this issue:
1 2 3 4 5 6 7 | $parent = Doctrine_Query::create()->from('Parent p')->where('p.id = ?', 1)->limit(1)->fetchOne(); print_r($parent->getChild()->toArray()); $child = new Child(); $child->setParentId($parent->getId()); $child->save(); $parent->refresh(true); /* see note! */ print_r($parent->getChild()->toArray()); |