Zend Framework 2 Doctrine prePersist 不起作用

Zend Framework 2 Doctrine prePersist not working

我正在尝试在我的实体中使用学说侦听器 prePersist class,但它不起作用。

我的实体class是

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
use Application\Entity\AbstractEntity;
use Freedom\Logger\InfoLogger;
use Application\Entity\UserRoles;
use Application\Entity\Countries;

/**
 * Users
 *
 * @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="user_role_id", columns={"user_role_id"}), @ORM\UniqueConstraint(name="country_id", columns={"country_id"})})
 * @ORM\Entity(repositoryClass="Application\Entity\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Users extends AbstractEntity
{
/**
 * @var integer
 *
 * @ORM\Column(name="user_id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $userId;

/**
 * @var string
 *
 * @ORM\Column(name="email_address", type="string", length=254, nullable=false)
 */
private $emailAddress;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=32, nullable=false)
 */
private $password;

/**
 * @var integer
 *
 * @ORM\Column(name="user_active", type="integer", nullable=false)
 */
private $userActive;

/**
 * @var string
 *
 * @ORM\Column(name="company_name", type="string", length=255, nullable=true)
 */
private $companyName;

/**
 * @var string
 *
 * @ORM\Column(name="address", type="text", length=65535, nullable=true)
 */
private $address;

/**
 * @var string
 *
 * @ORM\Column(name="town", type="string", length=255, nullable=true)
 */
private $town;

/**
 * @var integer
 *
 * @ORM\Column(name="post_code", type="string", length=20, nullable=true)
 */
private $postCode;

/**
 * @var \Application\Entity\UserRoles
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\UserRoles", inversedBy="users")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_role_id", referencedColumnName="user_role_id")
 * })
 */
private $userRole;

/**
 * @var \Application\Entity\Countries
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\Countries")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
 * })
 */
private $country;

/**
 * @var string
 *
 * @ORM\Column(name="date_created", type="datetime")
 */
private $dateCreated;

/**
 * @var string
 *
 * @ORM\Column(name="date_modified", type="datetime")
 */
private $dateModified;

/**
 * Get userId
 *
 * @return integer 
 */
public function getUserId()
{
    return $this->userId;
}

/**
 * Set emailAddress
 *
 * @param string $emailAddress
 * @return Users
 */
public function setEmailAddress($emailAddress)
{
    $this->emailAddress = $emailAddress;

    return $this;
}

/**
 * Get emailAddress
 *
 * @return string 
 */
public function getEmailAddress()
{
    return $this->emailAddress;
}

/**
 * Set password
 *
 * @param string $password
 * @return Users
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set userActive
 *
 * @param integer $userActive
 * @return Users
 */
public function setUserActive($userActive)
{
    $this->userActive = $userActive;

    return $this;
}

/**
 * Get userActive
 *
 * @return integer 
 */
public function getUserActive()
{
    return $this->userActive;
}

/**
 * Set companyName
 *
 * @param string $companyName
 * @return Users
 */
public function setCompanyName($companyName)
{
    $this->companyName = $companyName;

    return $this;
}

/**
 * Get companyName
 *
 * @return string 
 */
public function getCompanyName()
{
    return $this->companyName;
}

/**
 * Set address
 *
 * @param string $address
 * @return Users
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

/**
 * Get address
 *
 * @return string 
 */
public function getAddress()
{
    return $this->address;
}

/**
 * Set town
 *
 * @param string $town
 * @return Users
 */
public function setTown($town)
{
    $this->town = $town;

    return $this;
}

/**
 * Get town
 *
 * @return string 
 */
public function getTown()
{
    return $this->town;
}

/**
 * Set postCode
 *
 * @param integer $postCode
 * @return Users
 */
public function setPostCode($postCode)
{
    $this->postCode = $postCode;

    return $this;
}

/**
 * Get postCode
 *
 * @return integer 
 */
public function getPostCode()
{
    return $this->postCode;
}

/**
 * Set userRole
 *
 * @param \Application\Entity\UserRoles $userRole
 * @return Users
 */
public function setUserRole(\Application\Entity\UserRoles $userRole = null)
{
    $this->userRole = $userRole;

    return $this;
}

/**
 * Get userRole
 *
 * @return \Application\Entity\UserRoles 
 */
public function getUserRole()
{
    return $this->userRole;
}

/**
 * Set country
 *
 * @param \Application\Entity\Countries $country
 * @return Users
 */
public function setCountry(\Application\Entity\Countries $country = null)
{
    $this->country = $country;

    return $this;
}

/**
 * Get country
 *
 * @return \Application\Entity\Countries 
 */
public function getCountry()
{
    return $this->country;
}

/**
 * Get dateCreated
 * @return string
 */
public function getDateCreated()
{
    return $this->dateCreated;
}

/**
 * Set dateCreated
 * @param string $dateCreated
 * @return \Application\Entity\Users
 */
public function setDateCreated($dateCreated)
{
    $this->dateCreated = $dateCreated;
    return $this;
}

/**
 * Get dateModified
 * @return string
 */
public function getDateModified()
{
    return $this->dateModified;
}

/**
 * Set dateModified
 * @param string $dateModified
 * @return \Application\Entity\Users
 */
public function setDateModified($dateModified)
{
    $this->dateModified = $dateModified;
    return $this;
}

/**
 * @ORM\PrePersist
 */
protected function prePersist()
{
    die('prePersist');
    $this->dateCreated = date('Y m d H:i:s');
    return $this;
}
}

我在我的控制器中使用

$entityManager->persist($user);

$user 是上述实体

如您所见,prePersist 方法应该会终止,但这并没有发生。

我错过了什么?

非常感谢。

我刚刚弄明白了,prePersist 方法没有受到保护 public。