我试图通过两个学说回购使用两个嵌套查询来获取一些数据:
$user = $this->_objectManager->getRepository('\Application\Entity\User')->
findOneBy(array("hash" => $data));
$id_user = $user->getId();
$user_credits = $this->_objectManager->getRepository('\Application\Entity\UserCredits')->
findOneBy(array("user_id" => $id_user));
return $user_credits;
我在 module.config.php 中应用了一些 Hydrators:
'Application\\Entity\\User' => array(
'entity_identifier_name' => 'id',
'route_name' => 'client-api.rest.getcredits',
'route_identifier_name' => 'getcredits_id',
'hydrator' => 'Zend\\Stdlib\\Hydrator\\ArraySerializable',
),
'Application\\Entity\\UserCredits' => array(
'entity_identifier_name' => 'credits',
'route_name' => 'client-api.rest.getcredits',
'route_identifier_name' => 'getcredits_id',
'hydrator' => 'Zend\\Stdlib\\Hydrator\\ArraySerializable',
),
在每个学说实体中,我像这样配置 getArrayCopy():
用户:
public function getArrayCopy()
{
return array(
'id'=>$this->getId(),
'phone'=>$this->getPhone(),
);
}
用户积分:
public function getArrayCopy()
{
return array(
'id'=>$this->getUser(),
'credits'=>$this->getCredits(),
);
}
结果如下:
{"credits":100,"_embedded":{"id":{"id":10,"phone":123456789,"_links":{"self":{"href":"http:\/\/testserver.test\/getcredits\/10"}}}},"_links":{"self":{"href":"http:\/\/testserver.test\/getcredits\/100"}}}
但我实际上想要一个类似的结果:
{"credits":100,"id":10},"_links":......
我怎样才能做到这一点?为什么将 ID 嵌入其中?