Doctrine orm Zend Framework 2条令预科生不工作

Doctrine orm Zend Framework 2条令预科生不工作,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我试图在实体类中使用doctrine listener prePersist,但它不起作用 我的实体类是 <?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\User

我试图在实体类中使用doctrine listener prePersist,但它不起作用

我的实体类是

<?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;
}
}
$user是上述实体

正如您所看到的,prePersist方法应该会消亡,但这不会发生

我错过了什么


非常感谢。

我刚刚发现,prePersist方法是受保护的,不是公开的

$entityManager->persist($user);