ZF2 水合器和阵列
ZF2 hydrators and arrays
我对 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]->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 和 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 与第一次迭代中的值不同...
到循环结束时,数组中的每个元素都有最后一个实体...很奇怪。提前致谢:)
遇到了同样的问题。我假设您像我一样将此代码基于教程。好吧,问题出在教程中。
替换
$resultSet = new HydratingResultSet($this->hydrator, $this->entity);
和
$resultSet = new HydratingResultSet($this->hydrator, clone $this->entity);
这对我有用。
祝你好运。
几个月前我解决了我的问题。我找到了答案 here。问题是我的实体的属性是对象,而不是标量变量。然后,我需要实现克隆方法:
public function __clone() {
foreach ($this->attributes as &$attribute) {
$attribute = clone $attribute;
}
... clone of another variables that are objects
}
最后,这是一个关于克隆的问题,而不是关于 zf2 水化器和阵列的问题。
我对 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]->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 和 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 与第一次迭代中的值不同...
到循环结束时,数组中的每个元素都有最后一个实体...很奇怪。提前致谢:)
遇到了同样的问题。我假设您像我一样将此代码基于教程。好吧,问题出在教程中。
替换
$resultSet = new HydratingResultSet($this->hydrator, $this->entity);
和
$resultSet = new HydratingResultSet($this->hydrator, clone $this->entity);
这对我有用。
祝你好运。
几个月前我解决了我的问题。我找到了答案 here。问题是我的实体的属性是对象,而不是标量变量。然后,我需要实现克隆方法:
public function __clone() {
foreach ($this->attributes as &$attribute) {
$attribute = clone $attribute;
}
... clone of another variables that are objects
}
最后,这是一个关于克隆的问题,而不是关于 zf2 水化器和阵列的问题。