我在symfony中遇到了lifecyclecallbacks的一些问题。这是我的实体:
<?php
namespace MainBundle\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use AppBundle\Entity\Event;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table("users")
* @ORM\Entity(repositoryClass="MainBundle\Bundle\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser
{
public function __construct() {
$this->events = new ArrayCollection();
parent::__construct();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var \DateTime
*
* @ORM\Column(name="joined", type="datetime")
*/
private $joined;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime",nullable=true)
*/
private $updated;
/**
* events
*
* @var ArrayCollection
* @access private
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Event",mappedBy="user")
*/
private $events;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set joined
*
* @param \DateTime $joined
* @return User
*/
public function setJoined($joined)
{
$this->joined = $joined;
return $this;
}
/**
* Get joined
*
* @return \DateTime
*/
public function getJoined()
{
return $this->joined;
}
/**
* Set updated
*
* @param \DateTime $updated
* @return User
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set events
*
* @param \DateTime $events
* @return User
*/
public function setEvents($events)
{
$this->events = $events;
return $this;
}
/**
* Get events
*
* @return ArrayCollection
*/
public function getEvents()
{
return $this->events;
}
/**
* Add event
*
* @param Event $event
* @return User
*/
public function addEvent(Event $event)
{
$this->events->add($event);
return $this;
}
/**
* Remove event
*
* @param Event $event
* @return User
*/
public function removeEvent(Event $event)
{
$this->events->removeElement($event);
return $this;
}
/*
* @ORM/PrePersist
*/
public function PrePersist()
{
$this->joined = new \DateTime();
}
/*
* @ORM/PreUpdate
*/
public function PreUpdate()
{
$this->updated = new \DateTime();
}
}
我一直在寻找一段时间,但仍然没有发现这个问题。我尝试过这里的一些变化,例如使用区分大小写的规则进行实验(我的意思是在这两个方法周围使用大/小的情况)我还尝试用双星/**
开始块引号(至于这个我还是不知道它是否是是否有效)。如果这里需要更多文件,我会粘贴它们。
答案 0 :(得分:1)
你似乎有一些错别字。而不是:
@ORM/PrePersist
尝试:
@ORM\PrePersist
e.g:
/**
* @ORM\PrePersist
*/
public function PrePersist()
{
$this->joined = new \DateTime();
}
/**
* @ORM\PreUpdate
*/
public function PreUpdate()
{
$this->updated = new \DateTime();
}
感到惊讶的是这不是一个例外,但你去了。希望这可以帮助! :)