扩展FOSUserBundle用户实体时的生命周期回调问题 [英] Lifecycle Callback Issue When Extending FOSUserBundle User Entity

查看:116
本文介绍了扩展FOSUserBundle用户实体时的生命周期回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将FOSUserBundle首次导入到symfony2项目中,并且在扩展用户实体时注意到了一个问题。我使用prePersist和preUpdate生命周期回调添加了created_at和updated_at字段,但是这些方法没有被读取。

I've just imported the FOSUserBundle for the first time into a symfony2 project and I've noticed an issue when extending the user entity. I added created_at and updated_at fields with prePersist and preUpdate lifecycle callbacks but these methods are not being read.

如果我在构造函数中为这些字段设置了setter,那么填充字段(但显然这对update_at无效)。我添加的其他字段已经按预期工作。

If I put setters for these fields in the constructor then the fields are populated (but obviously this does not work correctly with updated_at). The other fields I have added have worked as expected.

您是否需要以某种方式扩展UserListener,以使生命周期事件能正常工作?

Do you need to extend the UserListener in some way to allow the lifecycle events to work correctly?

请在下面找到我的代码,任何帮助或建议将不胜感激。

Please find my code below, any help or advice would be greatly appreciated.

UserEntity:

UserEntity:

namespace Acme\UserExtensionBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Acme\UserExtensionBundle\Entity\User
 *
 * @ORM\Table(name="acme_user")
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */
class User extends BaseUser{

  /**
   * @var integer $id
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;

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

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

  ...

  public function __construct() {
    parent::__construct();
    $this->created_at = new \DateTime;
    $this->updated_at = new \DateTime;
  }

  /*
   * @ORM\preUpdate
   */
  public function setUpdatedTimestamp(){
    $this->updated_at = new \DateTime();
  }

  ...


推荐答案

快速浏览后,我只能发现一些注释名称的错误。

After a quick look I can only spot a little error with the case of the Annotations name.

它将被

@ORM\PreUpdate

而不是

@ORM\preUpdate

哪些IMHO应该在执行时导致错误。

which IMHO should lead to an error when executed.

无论如何,我建议您使用 http://symfony.com/doc/current/cookbook/doctrine/common_extensions.html

Anyway I would suggest you to use the DoctrineExtensionsBundle described in http://symfony.com/doc/current/cookbook/doctrine/common_extensions.html .

它附带一个时间戳(和更多有用的)行为,所以你不需要自己编写代码(重新发明)。

It comes with a Timestampable (and many more useful) behaviours so you do not need to code this on your own (reinventing the wheel).

我正在和FOSUserBundle一起使用它工作正常。这是我在用户实体中的定义如何:

I'm using it together with FOSUserBundle and it works fine. This is how my definition in the User Entity looks like:

 /**
 * @var \DateTime $created
 *
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(type="datetime")
 */
protected $created;

/**
 * @var \DateTime  $updated
 *
 * @Gedmo\Timestampable(on="update")
 * @ORM\Column(type="datetime")
 */
protected $updated;

这篇关于扩展FOSUserBundle用户实体时的生命周期回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆