- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的实体的 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');
}
}
<?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
最佳答案
我也有同样的问题。根据文档,您必须启用生命周期回调 http://symfony.com/doc/current/book/doctrine.html
/**
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
*/
class Product
{
// ...
}
关于Symfony2 ORM prePersist 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992549/
我在继承和 方面遇到了一些问题@PrePersist 注解。 我的源代码如下所示: _带有带注释的 updateDates() 方法的“基”类: @javax.persistence.Entity @
我有 GenericEntity 类,其中包含 @PrePersist 方法 onCreate(),它在将实体保存到数据库之前设置时间戳。它运行良好,但当我尝试测试我的 DAO 类时,我遇到了问题。
我是 Symfony2 的新手,我想知道 prePersist 和 preUpdate 事件有什么区别。看起来 prePersist 在我“保留”记录之前被“触发”,但是 preUpdate 什么时候
我如何模拟 @PrePersist 方法,例如我实例化的实体的 preInit() ? 我正在使用 TestNG。 EasyMock 是首选。 @Test(enabled = true) public
我的实体的 prePersist() 方法没有被调用。 username = $username; return $this; } /** * Get use
我用这段线来合并: partner = em.merge(partner); 这是在我的实体中: @PrePersist public void updateModificationDate(){
我有以下用于将用户存储在数据库中的实体,目前它仍然是 WIP,但我在创建新用户时遇到重复问题。每次我使用注册表单和 prePersist 添加新用户时,它都会复制 Roles 表中的值。在 addUs
我正在使用 Hibernate 和 MySQL 在 Spring MVC 中开发一个应用程序,但我遇到了一个问题。我正在尝试使用 @PrePersist 注释在我的 Java 实体中填充我最后修改的字
我在实现 Doctrine EventListener 时遇到问题。创建新发票时,InvoiceType 表单中包含一组项目(标题、价格、金额)。对于发票,在 price 字段中,我想插入所有购买产品
假设以下代码片段使用 @PrePersist 和 @PreUpdate 注释和 Joined-type 继承: @Entity @Inheritance(strategy=InheritanceTyp
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
我的 Symfony2 项目中有一个 Doctrine2 实体(称之为实体 A)。该实体与项目中的另一个实体(称为实体 B)具有 ManyToOne 关系。 实体 A 的状态属性为“事件”或“非事件”
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
如果未手动设置,我想自动设置其中一个实体字段。有没有办法检查这个?这些字段有一个默认值,所以我不能简单地比较这个值。我想知道学说是否维持值(value)是否改变以及我是否可以访问该信息。 此外,Doc
我编写了一个 @PrePersist 方法,该方法在保留我的 User 对象之前调用。该方法的目的是为用户进行一种预订,因此其他人无法获取他的电子邮件地址。因此,当用户即将被保留时,我说,“为用户#x
我有这个“架构”的“发布”实体/表: @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; @GeneratedValue(strat
我有一个事务性警报表和一个主警报类型表。我想在表中添加警报时发送一封电子邮件,所以我想我会使用 PrePersist。但是,在我的电子邮件中,我想包含一些包含在警报类型表中的信息。 我试图在 Aler
我的代码 @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class SiteMessage imple
我正在使用基本实体: @MappedSuperclass public class BaseEntity { private static final Logger L = LoggerFac
好吧,我是 symfony 的初学者,到目前为止,我开发了一个网站,用户可以在其中注册自己,并在登录后动态创建表单,但我的注册表单无法按预期工作。我的基本想法是当用户尝试注册自己时,预先检查他输入的客
我是一名优秀的程序员,十分优秀!