2015-02-10 50 views
1

我想通過兩個學說回購來獲取使用兩個嵌套查詢一些數據:ZF2,Apigility與學說和嵌入式Atributes問題

$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; 

我申請一些Hydrators在module.config.php:

'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

它嵌入是因爲你返回一個對象。

'id' => $this->getUser() 

在這種情況下idUser類型的對象和該對象將呈現嵌入因爲extractEmbeddedEntity方法被稱爲Hal插件的renderEntity方法內

只需改變爲:

'id' => $this->getUser()->getId() 

你會得到你想要的結果。