Php Symfony1.4和Doctrine1.2在执行时设置和获取子元素
我使用Symfony1.4和Doctrine1.2创建子对象,并在执行时打印它们。问题是我总是从第一个查询中获取子对象 我有以下模式:Php Symfony1.4和Doctrine1.2在执行时设置和获取子元素,php,doctrine,symfony-1.4,doctrine-1.2,Php,Doctrine,Symfony 1.4,Doctrine 1.2,我使用Symfony1.4和Doctrine1.2创建子对象,并在执行时打印它们。问题是我总是从第一个查询中获取子对象 我有以下模式: Parent: id name Child: id parent_id name 我的行动: 最初我有一个父母(id=1),没有孩子 我抓取一个父对象(id=1)并将该对象存储在$Parent 列出它的子对象 结果:无正常:如预期的那样 创建新的子项并将其父项设置为1 列出$parent的子对象 结果:noneOOPS:我希望新孩子5岁就
Parent:
id
name
Child:
id
parent_id
name
我的行动:
$Parent
无
正常:如预期的那样1
$parent
的子对象none
OOPS:我希望新孩子5岁就出生了强>$parent=doctor\u Query::create()->from('parent p')->其中('p.id=?',1)->limit(1)->fetchOne();/*从2*/
打印($parent->getChild()->toArray());/*从3*/
$child=newchild();
$child->setParentId($parent->getId());
$child->save();/*从4*/
打印($parent->getChild()->toArray());/*从6号开始*/
我已经尝试在最后一行之前重新获取$parent,但结果是一样的。我找到了解决此问题的方法:
$parent=doctor\u Query::create()->from('parent p')->其中('p.id=?',1)->limit(1)->fetchOne();
打印($parent->getChild()->toArray());
$child=newchild();
$child->setParentId($parent->getId());
$child->save();
$parent->refresh(true);/*见注*/
打印($parent->getChild()->toArray());
注意,来自学说的评论:
/**
* 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
*/