谁可以举个例子?
我有实体[WebGet(UriTemplate = "{id}")]
public HttpResponseMessage isTest(int id)
{
Model model = Model.table.Where(p => p.Id == id).FirstOrDefault();
if (model != null)
{
//return Request.CreateResponse<Model>(HttpStatusCode.OK, model);
//or
return Request.CreateResponse(HttpStatusCode.OK, model);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Model Not Found");
}
}
和Project
- 许多事件到一个项目,所以当状态项目更改为Event
时,我需要为此项目创建一个事件。
在控制器中:
'closed_by_client'
项目实体:
$project = $this
->getDoctrine()
->getManager()
->getRepository('ArtelProfileBundle:Project')
->find($id);
$project->setCurrentStatus('closed_by_client');
$manager->persist($project);
$manager->flush();
我认为我可能会做这样的事情,但实体/**
* Project.
*
* @ORM\Table(name="project")
* @ORM\HasLifecycleCallbacks
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\ProjectRepository")
* @ExclusionPolicy("all")
*/
class Project
{
const STATE_TO_BE_INDEXED_IN_ELA_NOT_APPROVED = 'not_approved';
const STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT = 'closed_by_client';
use Timestampable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @Expose()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="current_status", type="string", length=100, nullable = true)
* @Expose()
* @Type("string")
*/
protected $currentStatus = 'not_approved';
/**
* @ORM\OneToMany(targetEntity="CodeEvents", mappedBy="project", cascade={"persist", "remove"})
*/
protected $events;
/**
* @ORM\PrePersist()
*/
public function PrePersist(){
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event
->setProject($this)
->setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
;
}
}
/**
* @ORM\PreFlush()
*/
public function PreFlush(){
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event
->setProject($this)
->setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
;
}
}
没有在DB中刷新:
Event
我添加
/**
* @ORM\PrePersist()
*/
public function PrePersist(){
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event
->setProject($this)
->setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
;
}
}
/**
* @ORM\PreFlush()
*/
public function PreFlush(){
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event
->setProject($this)
->setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
;
}
}
但是当flush实体有错误时
/**
* @ORM\PreFlush()
*/
public function PreFlush(LifecycleEventArgs $event){
$entityManager = $event->getEntityManager();
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event
->setProject($this)
->setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
;
$entityManager->persist($event);
$entityManager->flush();
}
}
如何为事件创建侦听器 - 实体'closed_by_client'的项目更改状态 - 创建实体事件?
我没有创建早期事件,如何为此业务逻辑创建事件?
答案 0 :(得分:0)
考虑到prePersist和preFlush方法属于Project类本身。要将其添加到您的活动对象,您只需使用$this
:
public function PrePersist(){
if($this->getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
$event = new CodeEvents();
$event->setProject($this);
}
}
答案 1 :(得分:0)
在标题中,您询问Symfony事件监听器,但实际上您使用的是doctrine事件监听器。
我的笔记在这里:
1,您需要创建一个symfony2事件并从您的学说事件中触发。如果你想解雇symfony2事件会使事情变得复杂,因为你需要访问框架事件调度程序,你需要将它注入你的LifeCycleEventListener。 所以:
2,我会考虑检查状态字段是否完全更改,否则当Project实体的任何其他属性更改且状态已经是“close_by_client”时,您将创建许多CodeEvent()实例。 看这里: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-callbacks-event-argument