如何在onPropertyChanged上实现NotifyPropertyChanged方法?

时间:2016-10-07 15:04:51

标签: symfony events doctrine-orm

如何实现NotifyPropertyChanged以检索实体更改(在带有学说的Symfony 2.8项目中)?

我找到了几个例子,但它们并不完整:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/change-tracking-policies.html

http://www.boostr.in/26896/is-there-a-built-in-way-to-get-all-of-the-changed-updated-fields-in-a-doctrine-2%23googleads

对我来说,目前尚不清楚我是否必须实现自己的onPropertyChanged函数,怎么做?我想重新检查属性更改,命名控制器中的最后一个属性和当前属性,并将它们格式化为用户的消息。

当我在文档中搜索时,NotifyPropertyChanged不会被描述或描述得最低:

http://symfony2-document.readthedocs.io/en/latest/search.html?q=NotifyPropertyChanged&check_keywords=yes&area=default

http://api.symfony.com/2.8/search.html?search=NotifyPropertyChanged

http://docs.w3cub.com/symfony~2.8/

http://phpdox.de/demo/Symfony2/interfaces/Doctrine_Common_NotifyPropertyChanged.xhtml

写成addPropertyChangedListener() — Adds a listener that wants to be notified about property changes。我不明白我应该如何将自定义代码放在这个监听器中,以及如何以这种方式执行这个监听器,我可以检索实体对控制器操作的更改。

// src \ MeetingBundle \ Entity \ EventLisObject.php

<?php

namespace MeetingBundle\Entity;


use Doctrine\Common\NotifyPropertyChanged;
use Doctrine\Common\PropertyChangedListener;

abstract class EventLisObject implements NotifyPropertyChanged
{
    private $listeners = array();

    public function addPropertyChangedListener(PropertyChangedListener $listener) {
        $this->listeners[] = $listener;
    }

    /** Notifies listeners of a change. */
    protected function onPropertyChanged($propName, $oldValue, $newValue) {
        if ($this->listeners) {
            foreach ($this->listeners as $listener) {
                $listener->propertyChanged($this, $propName, $oldValue, $newValue);
            }
        }
    }
}

// C src \ MeetingBundle \ Entity \ Event.php

<?php

namespace MeetingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Event
 *
 * @ORM\Table(name="tevent", indexes={@ORM\Index(columns={"keywords"}, flags={"fulltext"})})
 * @ORM\Entity(repositoryClass="MeetingBundle\Repository\EventRepository" )
 */
class Event extends EventLisObject
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $title;

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Event
     */
    public function setTitle($title) {
        // $this->title = $title; 
        if( $title != $this->title ) {
        $this->_onPropertyChanged("title", $this->title, $title);
        $this->title = $title; } 

        return $this;
    }

// src \ MeetingBundle \ Controller \ EventMapControllerACL.php

....

/**
 * Event controller.
 *
 * @Route("/eventmapacl")
 */
class EventMapControllerACL extends Controller
{

    /**
     * @Route("/edit/{id}", name="event_jsMap_edit")
     * @Method("GET|POST")
     * @Template("MeetingBundle:Event:ev_jsMap_edit.html.twig")
     */
    public function editAction($id, Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('MeetingBundle:Event')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Event entity.');
        }

        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);
     //   $deleteForm = $this->createDeleteForm($id);

        if ($editForm->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
             // here or before flush or before persist i would like to estimate the changes and generate the message to user informing about them.
            return $this->redirect($this->generateUrl('event_jsMap_edit', array('id' => $entity->getId())));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        );
    }

0 个答案:

没有答案