Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second Level cache doesn't work properly with OneToOne association and inheritance #5562

Closed
rafrsr opened this issue Dec 18, 2015 · 1 comment
Assignees
Labels
Milestone

Comments

@rafrsr
Copy link

rafrsr commented Dec 18, 2015

Better explain with an example, see the following entities

Merchant:

/**
 * @ORM\Entity()
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="dealers")
 */
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")
     */
    protected $manager;

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

Manager:

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

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

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

and User:

/**
 * @ORM\Table(name="users")
 * @ORM\Entity()
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorMap({
 *      "MANAGER"  = "AppBundle\Entity\Manager",
 * })
 */
abstract class User
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

And the following code to test the problem

//creating entities
$merchant = new Merchant();
$merchant->setName('Merchant');
$manager = new Manager();
$manager->setUsername('username');
$manager->setMerchant($merchant);
$merchant->setManager($manager);

//persist
$em->persist($merchant);
$em->persist($manager);

$em->flush();
$em->clear();
$id = $merchant->getId();

//load from db/cache
$merchant = $doctrine->getManager()->find(Merchant::class, $id);

//should return: `current: username`
echo "current: " . $merchant->getManager()->getUsername() . "\n";

//update
$merchant->setName(mt_rand()); //update merchant randomly
$merchant->getManager()->setUsername('usernameUPDATE');
$doctrine->getManager()->flush();
$doctrine->getManager()->clear();

$merchant = $doctrine->getManager()->find(Merchant::class, $id);

//should return: `updated: usernameUPDATE`
echo "updated: " . $merchant->getManager()->getUsername() . "\n";

The above code should output:

current: username
updated: usernameUPDATE

but return

current: username
updated: username

the current data of the entity is not up to date in any case the database is updated fine.

However doing the following changes the above code works fine.

  • removing the inheritance in the manager
/**
 * @ORM\Entity()
 * @ORM\Table(name="users")
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="managers")
 */
class Manager
{

OR

  • removing the following line
$merchant->setName(mt_rand()); //update merchant randomly

When the merchant is updated with the manager then the second level cache don't update correctly the manager, but this only happen if the class have inheritance.

@lcobucci
Copy link
Member

@rafrsr thanks for the use case, I manage to find and solve the issue.

Sending a PR soon 😉

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

No branches or pull requests

3 participants