1

I'm trying to fetch some data using two nested queries through two 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;

I apply some Hydrators in the 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',
            ),

And in each doctrine Entity I configure the getArrayCopy() like this:

User:

public function getArrayCopy()
    {
        return array(
            'id'=>$this->getId(),
            'phone'=>$this->getPhone(),
        );
    }

UserCredits:

 public function getArrayCopy()
    {
        return array(
            'id'=>$this->getUser(),
            'credits'=>$this->getCredits(),
        );
    }

And here is the result:

{"credits":100,"_embedded":{"id":{"id":10,"phone":123456789,"_links":{"self":{"href":"http:\/\/testserver.test\/getcredits\/10"}}}},"_links":{"self":{"href":"http:\/\/testserver.test\/getcredits\/100"}}}

But I actually would like a result something like:

{"credits":100,"id":10},"_links":......

How can I make this possible? Why ID is embedding into it?

SirLouen
  • 137
  • 10

1 Answers1

0

It is embedded because you return an object.

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

In this case id will be an object of type User and that object will render embedded because of the extractEmbeddedEntity method being called inside the renderEntity method of the Hal plugin

Simply change to:

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

and you will get the result you want.

Wilt
  • 41,477
  • 12
  • 152
  • 203