The inheritance property of a Doctrine class class has an invalid value when requested

I use two doctrine concepts in my model:

  • Class Inheritance
  • Lifecycle Callbacks

When I load an inherited instance of the parent class, the parent property that is updated with the lifecycle callback is incorrect, well, not the value that I have in the parent classes table of my database.

Parent class: User

use DateTime;

/**
 * User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="MyProject\CoreBundle\Repository\UserRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="entity_name", type="string")
 * @ORM\DiscriminatorMap({"member" = "Member", "administrator" = "Administrator"})
 * @ORM\HasLifecycleCallbacks()
 */
abstract class User implements AdvancedUserInterface, Serializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    protected $createdAt;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    protected $updatedAt;

    // ...

    public function __construct()
    {
        $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
        // ...

        return $this;
    }

    /**
     * @ORM\PreUpdate
     */
    public function onPreUpdate()
    {
        $this->updatedAt = new DateTime();
    }
}

Child Class: Member

/**
 * Member
 *
 * @ORM\Table(name="members")
 * @ORM\Entity(repositoryClass="MyProject\CoreBundle\Repository\MemberRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Member extends User
{
    // ...

    public function __construct()
    {
        parent::__construct();

        // ...

        return $this;
    }
}

Then in the controller

use MyProject\CoreBundle\Entity\Member;

public function someAction(Member $member)
{
    $em = $this->getDoctrine()->getManager();
    $member = $em->getRepository(Member::class)->find(1);

    exit(var_dump($member->getUpdatedAt()));
    // --> date of today, why?
}

Here is an excerpt from my database contents

// users
|----|---------------------|---------------------|-------------|
| id |     created_at      |      updated_at     | entity_name |
|----|---------------------|---------------------|-------------|
| 1  | 2016-01-01 00:00:00 | 2016-06-01 12:00:00 |    member   |
|----|---------------------|---------------------|-------------|

// members
|----|
| id |
|----|
| 1  |
|----|

When I debug my updatedAt property of my member, I expect the value to be a DateTime object that matches 2016-06-01 12:00:00, not the date of the day ...

Is it even related to loading inheritance? I canโ€™t understand where they came from.

+4
1

updatedAt User. __construct() User $this->createdAt = new DateTime() $this->updatedAt = new DateTime(). , , createdAt User updatedAt User . User ( __constructor):

use DateTime;

/**
 * User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="MyProject\CoreBundle\Repository\UserRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="entity_name", type="string")
 * @ORM\DiscriminatorMap({"member" = "Member", "administrator" = "Administrator"})
 * @ORM\HasLifecycleCallbacks()
 */
abstract class User implements AdvancedUserInterface, Serializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    protected $createdAt;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    protected $updatedAt;

    // ...

    /**
     * @ORM\PrePersist
     */
    public function onPrePersist()
    {
        $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
    }

    /**
     * @ORM\PreUpdate
     */
    public function onPreUpdate()
    {
        $this->updatedAt = new DateTime();
    }
}
-1

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


All Articles