【问题标题】:ZF2 hydrators and arraysZF2 水合器和阵列
【发布时间】:2015-05-18 02:25:15
【问题描述】:

我对 ArraySerializable 水合器和数组有疑问。我有这个代码:

$users = array();
$produtos = array();
$ros = $this->roService->findAllEntities();
foreach ($ros as $ro) {
    $users[] = $this->usuarioService->findEntity($ro->attributes['idUsuario']->attribute);
    $produtos[] = $this->produtoService->findEntity($ro->attributes['idProduto']->attribute);
    var_dump($produtos[0]->attributes);
}

这是 var_dump($produtos[0]->attributes) 在两次迭代中的输出:

array (size=3)
'id' => 
  object(Application\Model\Attribute\Id)[307]
    protected 'name' => string 'id' (length=2)
    protected 'attribute' => string '2' (length=1)
    protected 'validators' => 
      array (size=0)
        empty
'dataHoraCadastro' => 
  object(Application\Model\Attribute\DataHoraCadastro)[308]
    protected 'name' => string 'dataHoraCadastro' (length=16)
    protected 'attribute' => string '2015-03-07 14:03:37' (length=19)
    protected 'validators' => 
      array (size=0)
        empty
'nome' => 
  object(Application\Model\Attribute\Nome)[309]
    protected 'name' => string 'nome' (length=4)
    protected 'validators' => 
      array (size=1)
        0 => 
          object(Application\Validator\StringLengthValidator)[310]
            ...
    protected 'minimoCaracteres' => int 3
    protected 'maximoCaracteres' => int 70
    protected 'attribute' => string 'Produto 1' (length=4)

array (size=3)
'id' => 
  object(Application\Model\Attribute\Id)[307]
    protected 'name' => string 'id' (length=2)
    protected 'attribute' => string '4' (length=1)
    protected 'validators' => 
      array (size=0)
        empty
'dataHoraCadastro' => 
  object(Application\Model\Attribute\DataHoraCadastro)[308]
    protected 'name' => string 'dataHoraCadastro' (length=16)
    protected 'attribute' => string '2015-03-07 14:03:37' (length=19)
    protected 'validators' => 
      array (size=0)
        empty
'nome' => 
  object(Application\Model\Attribute\Nome)[309]
    protected 'name' => string 'nome' (length=4)
    protected 'validators' => 
      array (size=1)
        0 => 
          object(Application\Validator\StringLengthValidator)[310]
            ...
    protected 'minimoCaracteres' => int 3
    protected 'maximoCaracteres' => int 70
    protected 'attribute' => string 'Produto 2' (length=9)

$users$produtos$ros 是实体数组。 findEntityfindAllEntities 方法的代码是:

/**
 * @inheritDoc
 */
public function findEntity($id) {
    $result = $this->executeFindSql($id);

    if ($result instanceof ResultInterface && $result->isQueryResult()) {
        return $this->hydrator->hydrate($result->current(), $this->entity);
    }
}

/**
 * @inheritDoc
 */
public function findAllEntities() {
    $result = $this->executeFindSql();

    if ($result instanceof ResultInterface && $result->isQueryResult()) {
        $resultSet = new HydratingResultSet($this->hydrator, $this->entity);

        return $resultSet->initialize($result);
    }

    return array();
}

问题在于 $produtos 和 $users 数组在调用 findEntity 方法时会覆盖整个数组。似乎在每次迭代中,整个数组都会被最后一个实体替换。然后,在第二次迭代中,$produtos 数组的索引 0 与第一次迭代中的值不同......

在循环结束时,数组中的每个元素都有最后一个实体……很奇怪。在此先感谢:)

【问题讨论】:

    标签: php arrays zend-framework2 clone


    【解决方案1】:

    遇到了同样的问题。我假设您像我一样根据本教程编写此代码。问题出在教程中。

    替换

    $resultSet = new HydratingResultSet($this->hydrator, $this->entity);

    $resultSet = new HydratingResultSet($this->hydrator, clone $this->entity);

    这对我有用。

    祝你好运。

    【讨论】:

    • 你是否也为这条线添加了clone $this->entity return $this->hydrator->hydrate($result->current(), $this->entity);
    • 你需要把clone 换成$this->entity
    • 首先,我把clone $this->entity放在$resultSet = new HydratingResultSet($this->hydrator, $this->entity)线上。然后,我也拨打了return $this->hydrator->hydrate($result->current(), $this->entity);。但是,还没有成功。
    【解决方案2】:

    几个月前我解决了我的问题。我找到了答案here。问题是我的实体的属性是对象,而不是标量变量。然后,我需要实现克隆方法:

    public function __clone() {
        foreach ($this->attributes as &$attribute) {
            $attribute = clone $attribute;
        }
        ... clone of another variables that are objects
    }
    

    最后,这是一个关于克隆的问题,而不是关于 zf2 水合器和阵列的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多