【问题标题】:Symfony1.4 and Doctrine1.2 setting and getting child elements in execution timeSymfony 1.4 和 Doctrine 1.2 在执行时设置和获取子元素
【发布时间】:2013-09-17 20:42:02
【问题描述】:

我正在使用 Symfony1.4 和 Doctrine1.2 创建子对象并在执行时打印它们。问题是我总是从第一个查询中获取子对象。

我有以下架构:

Parent:
  id
  name

Child:
  id
  parent_id
  name

我的操作:

  1. 最初我有一个 父母(id=1)和没有 孩子
  2. 我获取了一个Parent (id=1) 并将该对象存储在$parent
  3. 列出它的子对象
  4. 结果:none OK:如预期的那样
  5. 创建一个新的Child并将其父级设置为1
  6. 列出$parent对象
  7. 结果:none 哎呀:我期待 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,但结果是一样的。

【问题讨论】:

    标签: php doctrine symfony-1.4 doctrine-1.2


    【解决方案1】:

    我找到了解决此问题的方法:

    $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 的 cmets:

    /**
    * 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
    */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多