我尝试使用两个嵌套查询通过两个doctrine repos获取一些数据:
$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中应用了一些Hydrator:
'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(),
);
}
UserCredits:
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嵌入其中?
答案 0 :(得分:0)
它是嵌入式的,因为您返回一个对象。
'id' => $this->getUser()
在这种情况下,id
将是User
类型的对象,并且该对象将呈现嵌入,因为extractEmbeddedEntity
方法在renderEntity
方法中被调用{ {1}}插件
只需更改为:
Hal
你会得到你想要的结果。