Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Receiving parents (or at least their IDs) #45

Closed
intellix opened this issue Feb 5, 2014 · 2 comments
Closed

Receiving parents (or at least their IDs) #45

intellix opened this issue Feb 5, 2014 · 2 comments

Comments

@intellix
Copy link

intellix commented Feb 5, 2014

So I have a User like so:

/**
 * @ODM\Document
 */
class User
{
    /**
     * @ODM\Id
     */
    private $id;

    /**
     * @ODM\ReferenceMany(targetDocument="Db\Document\Message", mappedBy="sentBy")
     */
    private $messagesSent;

    /**
     * @ODM\ReferenceMany(targetDocument="Db\Document\Message", mappedBy="receivedBy")
     */
    private $messagesReceived;
}

Who can send and receive messages, which are referencing one another using DbRef:

/** 
 * @ODM\Document
 */
class Message
{
    /** @ODM\Id */
    private $id;

    /** @ODM\Field(type="date") */
    private $dateTimeSent;

    /** @ODM\Field(type="string") */
    private $text;

    /** @ODM\ReferenceOne(targetDocument="Db\Document\User", inversedBy="messagesSent") */
    private $sentBy;

    /** @ODM\ReferenceOne(targetDocument="Db\Document\User", inversedBy="messagesReceived") */
    private $receivedBy;
}

The issue I'm having is that when I grab messages or a single message, the sentBy and receivedBy are both NULL in the rest response. The data is there and linking correctly as if I can get it lazily using regular doctrine like: $message->getSentBy()->getId()

Looking at the Hydrator strategies I can see that CollectionExtract is for displaying an embedded list of child nodes and I'm thinking CollectionLink should be for the other way around (referencing the parent), but it's always NULL.

My config looks like:

'zf-hal' => array(
    'metadata_map' => array(
        'Db\\Document\\User' => array(
            'route_identifier_name' => 'user_id',
            'entity_identifier_name' => 'id',
            'route_name' => 'scanner.rest.user',
            'hydrator' => 'Db\\Document\\UserHydrator',
        ),
        'Db\\Document\\Message' => array(
            'route_identifier_name' => 'message_id',
            'entity_identifier_name' => 'id',
            'route_name' => 'scanner.rest.message',
            'hydrator' => 'Db\\Document\\MessageHydrator',
        ),
    ),
),
'zf-rest-doctrine-hydrator' => array(
    'Db\\Document\\UserHydrator' => array(
        'entity_class' => 'Db\\Document\\User',
        'object_manager' => 'doctrine.documentmanager.odm_default',
        'by_value' => false,
        'strategies' => array(
            'messagesSent' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionExtract',
            'messagesReceived' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionExtract',
        )
    ),
    'Db\\Document\\MessageHydrator' => array(
        'entity_class' => 'Db\\Document\\Message',
        'object_manager' => 'doctrine.documentmanager.odm_default',
        'by_value' => false,
        'strategies' => array(
            'sentBy' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionLink',
            'receivedBy' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionLink',
        ),
    ),
),
'zf-rest-doctrine-resource' => array(
    'Scanner\\V1\\Rest\\User\\UserResource' => array(
        'object_manager' => 'doctrine.documentmanager.odm_default',
        'hydrator' => 'Db\\Document\\UserHydrator',
    ),
    'Scanner\\V1\\Rest\\Message\\MessageResource' => array(
        'object_manager' => 'doctrine.documentmanager.odm_default',
        'hydrator' => 'Db\\Document\\MessageHydrator',
    ),
),

Should I be providing somehting more than just /api/messages to get the parent's IDs or should they just be there? If I dump the response from the ObjectManager get('Entity', $id) the IDs are null, but they're treated as though they're lazy so you have to get it manually.

@TomHAnderson
Copy link
Contributor

@intellix What's the status of this? Can you give an update using the latest code?

@TomHAnderson
Copy link
Contributor

Any movement on this @intellix or can I close this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants