ZF2,Apigility with Doctrine和Embedded Atributes问题

时间:2015-02-10 22:37:42

标签: php zend-framework doctrine-orm zend-framework2 apigility

我尝试使用两个嵌套查询通过两个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嵌入其中?

1 个答案:

答案 0 :(得分:0)

它是嵌入式的,因为您返回一个对象。

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

在这种情况下,id将是User类型的对象,并且该对象将呈现嵌入,因为extractEmbeddedEntity方法在renderEntity方法中被调用{ {1}}插件

只需更改为:

Hal

你会得到你想要的结果。