16

我的实体的 prePersist() 方法没有被调用。

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Users
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255, nullable=false)
     */
    private $username;

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

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

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

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

    /**
     * @var boolean
     *
     * @ORM\Column(name="active", type="boolean", nullable=false)
     */
    private $active;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="modified", type="datetime", nullable=false)
     */
    private $modified;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;



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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * 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 active
     *
     * @ORM\PrePersist
     * @param boolean $active
     * @return Users
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Users
     */
    public function setCreated()
    {
        $this->created = $created;

        return $this;        
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set modified
     *
     * @param \DateTime $modified
     * @return Users
     */
    public function setModified()
    {
        $this->modified = $modified;

        return $this;
    }

    /**
     * Get modified
     *
     * @return \DateTime 
     */
    public function getModified()
    {
        return $this->modified;
    }

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

    /**
    * @ORM\PrePersist
    * @ORM\PreUpdate
    */
    public function prePersist(){

      $this->active = 0;
      $this->created = date('Y-m-d H:i:s');
      $this->modified = date('Y-m-d H:i:s');

    }
}

这是我的 Form/UsersType.php 代码

<?php
namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class UsersType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username','text', array('required' => true))
            ->add('firstname','text', array('required' => true))
            ->add('lastname','text', array('required' => true))
            ->add('email', 'email', array('label' => 'E-Mail'))
            ->add('password', 'password', array('label' => 'Password'))
            ->add('password_confirmation', 'password', array('mapped' => false)) // Added virtual field on form
/*                
            ->add('active','hidden',array('data' => ''))
            ->add('created','hidden',array('data' => ''))
            ->add('modified','hidden',array('data' => ''))
 * 
 */
        ;        
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Users'
        ));
    }

    public function getName()
    {
        return 'acme_demobundle_userstype';
    }
}

我已经注释掉了这三个活动、创建和修改的字段,以便不在表单中显示它们。

但我收到错误:

An exception occurred while executing 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["neeraj", "neeraj", "kumar", "neeraj.kumar@test.com", "p@ssw0rd", null, null, null]:

我在实体中缺少什么?

堆栈跟踪

Stack Trace (Plain Text)  -

[1] Doctrine\DBAL\DBALException: An exception occurred while executing 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["neeraj", "neeraj", "kumar", "neeraj.kumar@rsystems.com", "p@ssw0rd", null, null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'active' cannot be null
    at n/a
        in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php line 47

    at Doctrine\DBAL\DBALException::driverExceptionDuringQuery(object(PDOException), 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', array('neeraj', 'neeraj', 'kumar', 'neeraj.kumar@rsystems.com', 'p@ssw0rd', null, null, null))
        in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 140

    at Doctrine\DBAL\Statement->execute()
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 277

    at Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts()
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 929

    at Doctrine\ORM\UnitOfWork->executeInserts(object(ClassMetadata))
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 318

    at Doctrine\ORM\UnitOfWork->commit(null)
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 355

    at Doctrine\ORM\EntityManager->flush()
        in C:\wamp\www\Symfony\src\Acme\DemoBundle\Controller\UsersController.php line 55

    at Acme\DemoBundle\Controller\UsersController->createAction(object(Request))
        in  line 

    at call_user_func_array(array(object(UsersController), 'createAction'), array(object(Request)))
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2774

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2748

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2878

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2179

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in C:\wamp\www\Symfony\web\app_dev.php line 28

[2] PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'active' cannot be null
    at n/a
        in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138

    at PDOStatement->execute(null)
        in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138

    at Doctrine\DBAL\Statement->execute()
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 277

    at Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts()
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 929

    at Doctrine\ORM\UnitOfWork->executeInserts(object(ClassMetadata))
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 318

    at Doctrine\ORM\UnitOfWork->commit(null)
        in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 355

    at Doctrine\ORM\EntityManager->flush()
        in C:\wamp\www\Symfony\src\Acme\DemoBundle\Controller\UsersController.php line 55

    at Acme\DemoBundle\Controller\UsersController->createAction(object(Request))
        in  line 

    at call_user_func_array(array(object(UsersController), 'createAction'), array(object(Request)))
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2774

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2748

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2878

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2179

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in C:\wamp\www\Symfony\web\app_dev.php line 28
4

5 回答 5

30

我也有同样的问题。根据文档,您必须启用生命周期回调http://symfony.com/doc/current/book/doctrine.html

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */
class Product
{
    // ...
}
于 2015-05-14T12:06:30.790 回答
6

我解决了清理缓存的问题!

php bin/console cache:clear
于 2018-05-31T14:35:23.750 回答
4

一个问题是这个功能。

/**
 * Set active
 *
 * @ORM\PrePersist
 * @param boolean $active
 * @return Users
 */
public function setActive($active)
{
    $this->active = $active;

    return $this;
}

使用时不能将参数传递给函数@PrePersist

还有这个功能

/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function prePersist(){

  $this->active = 0;
  $this->created = date('Y-m-d H:i:s');
  $this->modified = date('Y-m-d H:i:s');

}

失败,因为您正在为 DateTime 字段分配字符串值。尝试这个:

/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function prePersist()
{

  $this->active = false;
  $this->created = new \DateTime();
  $this->modified = new \DateTime();

}
于 2013-08-01T23:08:35.257 回答
1

我有同样的问题,因为我的方法是私有的,所以确保带有@ORM\PrePersist()注释的方法是公共的。

于 2019-05-01T18:59:43.457 回答
-4

@ORM\PrePersist缺少括号,应该是 @ORM\PrePersist()

于 2014-07-10T23:46:03.890 回答