One to many relation return partial collection of object #5270

Open
opened 2026-01-22 15:03:08 +01:00 by admin · 6 comments
Owner

Originally created by @hlecuyer on GitHub (Sep 22, 2016).

I have a strange behavior with doctrine 2.5 . I have a one to many relationship who doesn't return the expected result.

I have the following entities :

/**
 * @Entity @Table(name="orders")
 **/
class Order
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

//...

    /**
     * @var OrderActors
     * @OneToMany(targetEntity="Models\Orders\OrderActors", mappedBy="order", cascade={"remove"}, fetch="EAGER")
     **/
    protected $orderActors;

    /**
     * @return OrderActors
     */
    public function getOrderActors()
    {
        return $this->orderActors;
    }

}
/**
 * @Entity @Table(name="order_actors")
 **/
class OrderActors
{
    const OWNER = 'orderOwner';
    const CHARTERED = 'chartered';
    const READ_ONLY = 'read_only';

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     * **/
    protected $id;

    /**
     * @var Order
     * @ManyToOne(targetEntity="Models\Order", inversedBy="orderActors", fetch="EAGER")
     * @JoinColumn(name="order_id", referencedColumnName="id")
     **/
    protected $order;

    /**
     * @var string
     * @Column(type="string")
     **/
    protected $role;

//....
}

And when call $order->getOrderActors(), there is missing object in the collection result althought they are in the database. It's always the same object missing, the one with the role owner.

To make it work, i have two workaround:

  1. to get the collection directly from the orderActor repository ($em->getRepository(OrderActor::class)->findBy(['order' => $order]))
  2. to load the entire object order collection with the role owner from my database with the queryBuilder.After the following query, then the call to $order->getOrderActors() works fine.
$qb = $this->entityManager->getRepository(Order::class)->createQueryBuilder('ordr');

$qb
        ->select('ordr')
        ->innerJoin('ordr.orderActors', 'orderActors')
        ->andWhere('orderActors.role =  :roleOwner')
        ->setParameter('roleOwner', OrderActors::OWNER)
        ;
        $query = $qb->getQuery()->getResult();

Last hack, if place a $em->clear() between two request, sometimes it works.

I couldn't find anything similar on the issues I found. If you have a clue, that would be very helpfull.

Originally created by @hlecuyer on GitHub (Sep 22, 2016). I have a strange behavior with doctrine 2.5 . I have a one to many relationship who doesn't return the expected result. I have the following entities : ``` php /** * @Entity @Table(name="orders") **/ class Order { /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; //... /** * @var OrderActors * @OneToMany(targetEntity="Models\Orders\OrderActors", mappedBy="order", cascade={"remove"}, fetch="EAGER") **/ protected $orderActors; /** * @return OrderActors */ public function getOrderActors() { return $this->orderActors; } } ``` ``` php /** * @Entity @Table(name="order_actors") **/ class OrderActors { const OWNER = 'orderOwner'; const CHARTERED = 'chartered'; const READ_ONLY = 'read_only'; /** * @Id * @Column(type="integer") * @GeneratedValue * **/ protected $id; /** * @var Order * @ManyToOne(targetEntity="Models\Order", inversedBy="orderActors", fetch="EAGER") * @JoinColumn(name="order_id", referencedColumnName="id") **/ protected $order; /** * @var string * @Column(type="string") **/ protected $role; //.... } ``` And when call `$order->getOrderActors()`, there is missing object in the collection result althought they are in the database. It's always the same object missing, the one with the role `owner`. To make it work, i have two workaround: 1. to get the collection directly from the orderActor repository (`$em->getRepository(OrderActor::class)->findBy(['order' => $order])`) 2. to load the entire object order collection with the role `owner` from my database with the queryBuilder.After the following query, then the call to `$order->getOrderActors()` works fine. ``` php $qb = $this->entityManager->getRepository(Order::class)->createQueryBuilder('ordr'); $qb ->select('ordr') ->innerJoin('ordr.orderActors', 'orderActors') ->andWhere('orderActors.role = :roleOwner') ->setParameter('roleOwner', OrderActors::OWNER) ; $query = $qb->getQuery()->getResult(); ``` Last hack, if place a $em->clear() between two request, sometimes it works. I couldn't find anything similar on the issues I found. If you have a clue, that would be very helpfull.
Author
Owner

@coudenysj commented on GitHub (Sep 27, 2016):

@hlecuyer Can you check the SQL used when calling $order->getOrderActors()?

@coudenysj commented on GitHub (Sep 27, 2016): @hlecuyer Can you check the SQL used when calling `$order->getOrderActors()`?
Author
Owner

@hlecuyer commented on GitHub (Sep 27, 2016):

I couldn't find this particular call in the list that I have. The only call referecing to the ordorActors entity where include with a join in one of the previous request. Is there a easy way to do so?

@hlecuyer commented on GitHub (Sep 27, 2016): I couldn't find this particular call in the list that I have. The only call referecing to the ordorActors entity where include with a join in one of the previous request. Is there a easy way to do so?
Author
Owner

@coudenysj commented on GitHub (Sep 27, 2016):

As you fetch the association eagerly, it should show up as a join in the initial query. Can you check the query when fetching the order(s)?

@coudenysj commented on GitHub (Sep 27, 2016): As you fetch the association eagerly, it should show up as a join in the initial query. Can you check the query when fetching the order(s)?
Author
Owner

@hlecuyer commented on GitHub (Sep 28, 2016):

Here it is. Note: I put the EAGER parameter in order to solve the problem, but it didn't change anything.

@hlecuyer commented on GitHub (Sep 28, 2016): [Here it is](https://gist.github.com/hlecuyer/6cde130a3dce781c0ba117bb7b3e84b8). Note: I put the EAGER parameter in order to solve the problem, but it didn't change anything.
Author
Owner

@KayKursawe commented on GitHub (Jan 22, 2020):

Any new findings on this? I'm having the same issue with doctrine/orm v2.6.1.
Seems to behave randomly somehow, running the same query several times will end up in different results.
I also checked the logs, nothing. Then I increased the php memory_limit, still no change.
What else can it be?

@KayKursawe commented on GitHub (Jan 22, 2020): Any new findings on this? I'm having the same issue with doctrine/orm v2.6.1. Seems to behave randomly somehow, running the same query several times will end up in different results. I also checked the logs, nothing. Then I increased the php memory_limit, still no change. What else can it be?
Author
Owner

@SimonVanacco commented on GitHub (Aug 3, 2023):

I had this issue but it was entirely my own fault : I created a query with a join relationship and a max result directive which does not work. The docs says so here

If you have this issue check that you did not make the same mistake as me and use a Paginator istead of setMaxResult !

@SimonVanacco commented on GitHub (Aug 3, 2023): I had this issue but it was entirely my own fault : I created a query with a **join** relationship and a **max result** directive which does not work. The docs says so [here](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#first-and-max-result-items-dql-query-only) If you have this issue check that you did not make the same mistake as me and use a Paginator istead of setMaxResult !
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5270