ZF2水合物和阵列

时间:2015-03-16 02:49:00

标签: php arrays zend-framework2 clone

我遇到了 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] - >属性)的输出:

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是实体数组。 findEntity findAllEntities 方法的代码为:

/**
 * @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不具有与第一次迭代中相同的值...

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

2 个答案:

答案 0 :(得分:1)

有同样的问题。我假设您像我一样在教程上使用此代码。那么问题出在教程中。

替换

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

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

这是为我做的。

祝你好运。

答案 1 :(得分:0)

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

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

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