Jimbo
Jimbo

Reputation: 26594

Doctrine oneToMany relationship results in null for foreign key on inverse entity when serializing

Relevant information: I'm using Symfony 2.8.x, Doctrine 2.4.8 and JMSSerializerBundle 1.0.

A Resource can have many Experiences. I'm making an API Call to retrieve a Resource and all associated Experiences.

However, the JSON I get back contains a null on $resource on the Experience entity.

I'm using the the following method to retrieve the Resource:

$this->findBy([], [], $limit, $offset);

I don't believe this should be an issue because I've tried setting the fetch mode to EAGER directly in the annotations - this still hasn't worked. I've also cleared the cache.

Resource.orm.yml

oneToMany:
    experiences:
        targetEntity: Experience
        mappedBy: resource
        fetch: EAGER

Experience.orm.yml

resource:
    targetEntity: Resource
    inversedBy: experiences
    joinColumn:
        name: resource_id
        referencedColumnName: id
        fetch: EAGER
    fetch: EAGER

See I've tried ALL the eager fetches!!

Response

The response I get when making an API request for this json:

{
    "resources": [{
        "id": 1,
        # SNIP #
        "experiences": [{
            "resource": null,
            "id": 1,
            # SNIP #
        }]
    }]
}

Note the null for resource!

Here's why I think it's a problem with lazy loading: the collection is an instance of Doctrine\ORM\PersistentCollection instead of just an array as I expect:

enter image description here

Why is this null?? What is my problem? Have I:

Upvotes: 4

Views: 798

Answers (3)

Aize
Aize

Reputation: 556

Maybe you need to upgrade Doctrine 2.4.8 to 2.5

Doctrine2 in ZF2 - DQL gives different result than findOneBy method

> "doctrine/doctrine-module":"dev-master",
> "doctrine/doctrine-orm-module": "dev-master",
> "doctrine/dbal":"2.5.*@dev",
> "doctrine/orm": "2.5.*@dev",

[UPDATE]

Getting a "true" object from a proxy object in doctrine2

Upvotes: 0

VaN
VaN

Reputation: 2210

This is probably the normal behavior for the API and its Serializer (model to JSON transformation).

Why would you need experiences.resource to be filled actually ? You already have it on the root level of the JSON.

If you still want experiences to fully contain their resource, you probably have to configure the Serializer and force it to go 1 step deeper in serialization. But most of the time, Serializers are configured this way to avoid infinite cycles (resource containing its experiences, containing its resource, containing its experiences, and so on..

Upvotes: 1

Vinícius Fagundes
Vinícius Fagundes

Reputation: 2041

One away to force Doctrine retrieve all related entities is creating a dql containing this entities.

$dql = "Select r, e From \MyNamespace\ToEntity\Resource r 
         Join r.experiences e";

$entityManager->createQuery($dql)->execute();

Upvotes: 0

Related Questions