Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZF2水合器和阵列_Php_Arrays_Zend Framework2_Clone - Fatal编程技术网

Php ZF2水合器和阵列

Php ZF2水合器和阵列,php,arrays,zend-framework2,clone,Php,Arrays,Zend Framework2,Clone,我对阵列可变量化的水合器和阵列有问题。我有以下代码: $users = array(); $produtos = array(); $ros = $this->roService->findAllEntities(); foreach ($ros as $ro) { $users[] = $this->usuarioService->findEntity($ro->attributes['idUsuario']->attribute); $pr

我对阵列可变量化的水合器和阵列有问题。我有以下代码:

$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\u 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
是实体数组。findEntityfindalentities方法的代码为:

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


循环结束时,数组中的每个元素都有最后一个实体…非常奇怪。提前感谢:)

也有同样的问题。我假设您像我一样基于教程编写此代码。问题在教程中

替换

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

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

那是为了我


祝你好运

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

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

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

您是否也将
clone$this->entity
放在这一行
return$this->hydrator->hydrator($result->current(),$this->entity)?您需要为
$this->entity
放置
clone
,首先,我将
clone$this->entity
放在
$resultSet=new HydratingResultSet($this->hydrator,$this->entity)
行上。然后,我在行
return$this->hydrator->hydrator($result->current(),$this->entity)上放置了一个参数也是。但是,却没有成功。