简体   繁体   English

ZF2水化器和阵列

[英]ZF2 hydrators and arrays

I have a problem with a ArraySerializable hydrator and arrays. 我对ArraySerializable水化器和阵列有问题。 I have this code: 我有以下代码:

$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);
}

Here is the output of var_dump($produtos[0]->attributes) on two iterations: 这是两次迭代的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 and $ros are arrays of entities. $users$produtos$ros是实体的数组。 The code of findEntity and findAllEntities methods are: 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();
}

The problem is that $produtos and $users arrays have the entire array overwritten on call of findEntity method. 问题在于$ produtos和$ users数组在调用findEntity方法时会覆盖整个数组。 Seems that on each iteration, the full array is replaced with the last entity. 似乎在每次迭代中,整个数组都被最后一个实体替换。 Then, in the second iteration, the index 0 of $produtos array don't have the same value that in the first iteration... 然后,在第二次迭代中,$ produtos数组的索引0的值与第一次迭代中的值不同...

By the end of loop, each element on the array have the last entity...very strange. 循环结束时,数组上的每个元素都有最后一个实体...非常奇怪。 Thanks in advance :) 提前致谢 :)

Had this same problem. 遇到了同样的问题。 I am assuming you based this code on the tutorial like I did. 我假设您像我一样基于教程编写此代码。 Well the problem is in the tutorial. 问题在教程中。

replace 更换

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

with

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

That did it for me. 对我来说就做到了。

Good Luck. 祝好运。

I solved my problem some months ago. 几个月前我解决了我的问题。 I found the answer here . 我在这里找到了答案。 The problem is that the attributes of my entities are objects, not scalar variables. 问题在于我实体的属性是对象,而不是标量变量。 Then, I needed implement the clone method: 然后,我需要实现clone方法:

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

In the end, this is a question more about clone than about zf2 hydrators and arrays. 最后,这是一个关于克隆的问题,而不是关于zf2水化器和阵列的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM