OneToOne second level cache load #5559

Open
opened 2026-01-22 15:11:16 +01:00 by admin · 14 comments
Owner

Originally created by @shustrik on GitHub (May 25, 2017).

/**
 * @ORM\Entity()
  */
class Merchant
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var Manager
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Manager", mappedBy="merchant")
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="my_region")
     */
    protected $manager;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    protected $name;

and

/**
 * @ORM\Entity()
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="my_region")
 */
class Manager extends User
{

    /**
     * @var string
     *
     * @ORM\Column()
     */
    protected $username;

    /**
     * @var Merchant
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Merchant", inversedBy="manager")
     */
    protected $merchant;

Manager from merchant is always loaded from database. I guess that the problem is in Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister:loadOneToOneEntity: only persister which gets objects from database is called in this method, but loading from cache is missing.

Originally created by @shustrik on GitHub (May 25, 2017). ```php /** * @ORM\Entity() */ class Merchant { /** * @var integer * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="IDENTITY") */ protected $id; /** * @var Manager * * @ORM\OneToOne(targetEntity="AppBundle\Entity\Manager", mappedBy="merchant") * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="my_region") */ protected $manager; /** * @var string * * @ORM\Column(name="name", type="string", length=255, nullable=false) */ protected $name; ```` and ```php /** * @ORM\Entity() * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="my_region") */ class Manager extends User { /** * @var string * * @ORM\Column() */ protected $username; /** * @var Merchant * * @ORM\OneToOne(targetEntity="AppBundle\Entity\Merchant", inversedBy="manager") */ protected $merchant; ``` Manager from merchant is always loaded from database. I guess that the problem is in Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister:loadOneToOneEntity: only persister which gets objects from database is called in this method, but loading from cache is missing.
Author
Owner

@lcobucci commented on GitHub (May 26, 2017):

@shustrik can you please reproduce this in a functional test case (based on master) and send us a PR with the failing test? You can find examples on 2a239be45e/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (May 26, 2017): @shustrik can you please reproduce this in a functional test case (based on `master`) and send us a PR with the failing test? You can find examples on https://github.com/doctrine/doctrine2/tree/2a239be45ec4e64c736e4277cde8bff376ef2402/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@shustrik commented on GitHub (May 29, 2017):

@lcobucci test added

@shustrik commented on GitHub (May 29, 2017): @lcobucci test added
Author
Owner

@martixy commented on GitHub (Jun 12, 2017):

I think I have the same problem. I've got User (non-owning) <-> A bunch of extensions(e.g. customer, designer, etc).

User will not cache. Even if I mark all of the associations and owning entities.
Amusingly, it also creates a situation where caching makes performance worse - when you have a cache hit for the owning entities, even if you make the query with eager fetch, it will retrieve the cached super entities, but then have to go and fetch the 1-1s individually for each cache hit.
Or rather, it will fetch them individually, until the first cache miss, then it will make the eager fetch query, loading all those associations eagerly.

@martixy commented on GitHub (Jun 12, 2017): I think I have the same problem. I've got `User (non-owning) <-> A bunch of extensions(e.g. customer, designer, etc)`. User will not cache. Even if I mark all of the associations and owning entities. Amusingly, it also creates a situation where caching makes performance _worse_ - when you have a cache hit for the owning entities, even if you make the query with eager fetch, it will retrieve the cached super entities, but then have to go and fetch the 1-1s individually for each cache hit. Or rather, it will fetch them individually, until the first cache miss, then it will make the eager fetch query, loading all those associations eagerly.
Author
Owner

@lcobucci commented on GitHub (Sep 2, 2017):

As mentioned in #6473 the test scenario was invalid since the query was not set to use L2C.

@lcobucci commented on GitHub (Sep 2, 2017): As mentioned in #6473 the test scenario was invalid since the query was not set to use L2C.
Author
Owner

@lcobucci commented on GitHub (Sep 2, 2017):

I'll be closing this issue as invalid, @martixy could please check if enabling L2C on the query solves your issue and, if it doesn't solve it, open a PR with a failing test case?

@lcobucci commented on GitHub (Sep 2, 2017): I'll be closing this issue as `invalid`, @martixy could please check if enabling L2C on the query solves your issue and, if it doesn't solve it, open a PR with a failing test case?
Author
Owner

@wtorsi commented on GitHub (Mar 21, 2018):

Hi, sorry for asking, but what is the status of this request?
I've also have this problem, also using L2C on query in 2.6.1 version of doctrine/orm.

I see that loadOneToOneEntity in Cache/AbstractEntityPersister does not use cache at all.

@wtorsi commented on GitHub (Mar 21, 2018): Hi, sorry for asking, but what is the status of this request? I've also have this problem, also using L2C on query in 2.6.1 version of doctrine/orm. I see that `loadOneToOneEntity` in Cache/AbstractEntityPersister does not use cache at all.
Author
Owner
@lcobucci commented on GitHub (Mar 21, 2018): @wtorsi did you check https://github.com/doctrine/doctrine2/issues/6470#issuecomment-326765202 and https://github.com/doctrine/doctrine2/issues/6470#issuecomment-326765320 ?
Author
Owner

@wtorsi commented on GitHub (Mar 21, 2018):

Yes, of course. In my case everything is cacheable.
Try to reproduce this error in another test case.

@wtorsi commented on GitHub (Mar 21, 2018): Yes, of course. In my case everything is cacheable. Try to reproduce this error in another test case.
Author
Owner

@shustrik commented on GitHub (Mar 21, 2018):

Hi. I added filed test with setCacheble. Maybe something is missing
@wtorsi @lcobucci

@shustrik commented on GitHub (Mar 21, 2018): Hi. I added filed test with setCacheble. Maybe something is missing @wtorsi @lcobucci
Author
Owner

@wtorsi commented on GitHub (Mar 22, 2018):

Yep, i found it.
2 test fails in the last commit.

@wtorsi commented on GitHub (Mar 22, 2018): Yep, i found it. 2 test fails in the last commit.
Author
Owner

@wtorsi commented on GitHub (Mar 22, 2018):

So, add some cases to my test.

I found 2 problems.

  1. There is one more request for each related entity either it is in cache in case if you are using QueryCacheBuilder. It's just a bug.

  2. In case when we have S<->T1, S<->T2, The cacheEntry, which is constructed just after DefaultQueryCache::get() (hydrating inside this method) - is not valid. This is my main problem.

@wtorsi commented on GitHub (Mar 22, 2018): So, add some cases to my test. I found 2 problems. 1. There is one more request for each related entity either it is in cache in case if you are using QueryCacheBuilder. It's just a bug. 2. In case when we have S<->T1, S<->T2, The cacheEntry, which is constructed just after DefaultQueryCache::get() (hydrating inside this method) - is not valid. This is my main problem.
Author
Owner

@ostrolucky commented on GitHub (Aug 10, 2018):

new test scenario is valid

@ostrolucky commented on GitHub (Aug 10, 2018): new test scenario is valid
Author
Owner

@cnkt commented on GitHub (Jan 7, 2020):

Is there any progress on this issue? We are having the same problem.

@cnkt commented on GitHub (Jan 7, 2020): Is there any progress on this issue? We are having the same problem.
Author
Owner

@FabianKoestring commented on GitHub (Mar 9, 2021):

Is there any progress on this issue? We are having the same problem.

Same here ....

@FabianKoestring commented on GitHub (Mar 9, 2021): > Is there any progress on this issue? We are having the same problem. Same here ....
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5559