我遇到了 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不具有与第一次迭代中相同的值...
在循环结束时,数组上的每个元素都有最后一个实体......非常奇怪。在此先感谢:)
答案 0 :(得分:1)
有同样的问题。我假设您像我一样在教程上使用此代码。那么问题出在教程中。
替换
$resultSet = new HydratingResultSet($this->hydrator, $this->entity);
带
$resultSet = new HydratingResultSet($this->hydrator, clone $this->entity);
这是为我做的。
祝你好运。答案 1 :(得分:0)
public function __clone() {
foreach ($this->attributes as &$attribute) {
$attribute = clone $attribute;
}
... clone of another variables that are objects
}
最后,这是一个关于克隆的问题,而不是关于zf2水合物和阵列的问题。