4

我在模型中使用了两个 Doctrine 概念:

  • 类表继承
  • 生命周期回调

当我加载父类的继承实例时,使用生命周期回调更新的父属性是错误的,不是我在数据库的父类表中拥有的值。

父 {abstract} 类:用户

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();
    }
}

子类:会员

/**
 * 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;
    }
}

然后在控制器中

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?
}

这是我的数据库内容的摘录

// 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  |
|----|

当我调试成员的 updatedAt 属性时,我希望该值是对应于的 DateTime 对象2016-06-01 12:00:00,而不是日期...

它甚至与继承加载有关吗?无法弄清楚这是从哪里来的。

4

1 回答 1

-1

updatedAt可能来自User课堂。在__construct()您的班级中,您使用和User填充数据。我认为这是一种不好的做法,因为应该在创建时填写,并在创建或更新时填写。尝试将您的课程更新为此(删除):$this->createdAt = new DateTime()$this->updatedAt = new DateTime()createdAtUserupdatedAtUserUser__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();
    }
}
于 2017-02-08T10:20:40.640 回答