我不明白为什么使用某些 Entity 对象我可以设置 Id 而对于其他对象我得到一个错误,并说我 Id 不能为空,我必须传递一个对象。
例如:
$log = new Log();
$log->setTypeId(1);
$log->setUserId(1);
$entityManager->persist($log);
$entityManager->flush();
如果我尝试上面的代码,我会收到错误消息:Integrity constraint violation: 1048 Column 'user_id' cannot be null。我必须首先创建 Type Object 和 de User 对象并传递它们:
$log->setType($TypeObject)
$log->setUser($UserObject)
但是对于其他实体对象,我直接赋值没有问题,这是为什么呢?
这是我的实体日志:
<?php
/**
* @Entity
* @Table(name="log")
* @HasLifecycleCallbacks
*/
class Log
{
/**
* @var type
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
*
* @var type
* @Column(type="integer")
*/
protected $user_id;
/**
*
* @var type
* @Column(type="integer")
*/
protected $type_id;
/**
*
* @var type
* @Column(type="datetime")
*/
protected $created;
/**
*
* @var type
* @ManyToOne(targetEntity="User", inversedBy="logs")
*/
protected $user;
/**
*
* @ManyToOne(targetEntity="Type", inversedBy="logs")
*/
protected $type;
public function getId()
{
return $this->id;
}
public function getUserId()
{
return $this->user_id;
}
public function getTypeId()
{
return $this->type_id;
}
public function getCreated()
{
return $this->created;
}
public function setUserId($userId)
{
$this->user_id = $userId;
}
public function setTypeId($typeId)
{
$this->type_id = $typeId;
}
public function setCreated($created)
{
$this->created = $created;
}
public function setUser($user)
{
$this->user = $user;
}
public function setType($type)
{
$this->type = $type;
}
/**
* @PrePersist
*/
public function prePersist()
{
$this->setCreated(new DateTime());
}
}
?>