我实际上得到了一个
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null
错误,因为没有执行PrePersist。我四处搜索,最大的问题似乎是确保类注释中的@HasLifecycleCallback和正在使用的函数中的@PrePersist。
我迷路了,为什么我的代码没有调用PrePersist?
这是实体文件
<?php
namespace RaffleToolsEntity;
use DoctrineORMMapping as ORM;
/**
*
* @Entity
* @HasLifecycleCallbacks
* @Table(name="raffleitems")
**/
class RaffleItem
{
/**
* @var int
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @var string
* @Column(type="string")
*/
protected $name;
/**
* @var string
* @Column(type="string")
*/
protected $image;
/**
* @var DateTime
* @Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* @var DateTime
* @Column(name="updated_at", type="datetime")
*/
protected $updatedAt;
/**
* @PrePersist
* @PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new DateTime('now'));
}
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getImage()
{
return $this->image;
}
public function setImage($image)
{
$this->image = $image;
}
public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
public function getCreatedAt()
{
return $this->createdAt;
}
public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
}
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function toArray()
{
return get_object_vars($this);
}
}
提前谢谢。
遗憾的是,这是配置设置中的一个拼写错误
$env = $injector->make(Env::class);
$paths = array(__DIR__.'/../');
$isDevMode = isset($enf['DEVELOPMENT']);
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => $env['DB_HOST'],
'user' => $env['DB_USER'],
'password' => $env['DB_PASS'],
'dbname' => $env['DB_NAME'],
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
在表示$isDevMode = isset($enf['DEVELOPMENT']);
$env拼写错误并始终返回false的行中。