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
是实体数组。findEntity和findalentities方法的代码为:
/**
* @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)上放置了一个参数代码>也是。但是,却没有成功。