我正在使用Symfony1.4和Doctrine1.2创建子对象并在执行时打印它们。问题是我总是从第一个查询中获取子对象。
我有以下架构:
Parent:
id
name
Child:
id
parent_id
name
我的运营:
$parent
none
确定:按预期 1
$parent
的儿童对象none
OOPS:我预计新孩子会从5开始。 代码:
$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. */
我已经尝试在最后一行之前重新抓取$ parent,但结果是一样的。
答案 0 :(得分:0)
我找到了解决这个问题的方法:
$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());
请注意,来自Doctrine的评论:
/**
* 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
*/