The Doctrine oneToMany relation results in a null value for the foreign key on the return object during serialization

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

A Resourcecan have a lot Experiences. I am creating an API call to receive Resourceand all associated Experiences.

However, the JSON that I return contains nullin $resourcefor the object Experience.

I use the following method to extract Resource:

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

I don't think this should be a problem because I tried to set the sampling mode to EAGERdirectly in the annotation - it still didn't work. I 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

Look, I tried ALL the hot selections!

Answer

, API json:

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

null !

, : Doctrine\ORM\PersistentCollection , :

enter image description here

null? ? :

  • , .
  • .
  • , - ?
  • - ?
+4
3

, , API Serializer ( JSON).

experiences.resource ? JSON.

, , , , 1 . , (, , , ..

+1

, Doctrine 2.4.8 2.5

Doctrine2 ZF2 - DQL , findOneBy

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

[]

"true" - doctrine2

0

, Doctrine , dql, .

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

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

Source: https://habr.com/ru/post/1662397/


All Articles