我正在尝试在数据库中插入一个新对象/行。对象是这样创建的:
$nodeaccess = new Nodeaccess(); // A by doctrine2 generated entity
$nodeaccess->setAccesslevel(0);
$nodeaccess->setDraw(0);
$nodeaccess->setUserid($userid);
$nodeaccess->setNodename($this->getUser()->getUsername() . ' Node');
$nodeaccess->setMac($node);
表的所有列都已设置。当我打印$nodeaccess->getUserid()
并打印$nodeaccess->getMac()
所需的结果时。而且它们都不是 null。
但是当对象像这样持久化时:
$em = $this->getDoctrine()->getManager();
$em->persist($nodeaccess);
$em->flush();
发生以下错误:
执行 'INSERT INTO nodeaccess (mac, userID, accessLevel, nodeName, draw) VALUES (?, ?, ?, ?, ?)' 时发生异常,参数为 {"1":null,"2":null,"3 ":0,"4":"示例节点","5":0}:
SQLSTATE [23000]:违反完整性约束:1048 列“mac”不能为空
mac 和 userID 组合是主键,它们也是外键。它们在模型中设置如下:
/**
* @var integer
*
* @ORM\Column(name="mac", type="bigint", nullable=false)
* @ORM\Id
*/
private $mac;
/**
* @var integer
*
* @ORM\Column(name="userID", type="integer", nullable=false)
* @ORM\Id
**/
private $userid;
公共访问器已实现,我尝试将字段更改为公共,但没有帮助。
更新 访问器:
public function getMac()
{
return $this->mac;
}
public function setMac($mac)
{
$this->mac = $mac;
}
public function getUserid()
{
return $this->userid;
}
public function setUserid($userid)
{
$this->userid = $userid;
}
更新 2
我已更改表,现在只有mac
字段为 NULL。新型号:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="mac", type="bigint", nullable=false)
*/
private $mac;
/**
* @var integer
*
* @ORM\Column(name="userID", type="integer", nullable=false)
**/
private $userid;
更新控制器动作:
public function inviteAction() {
$repository = $this->getDoctrine()
->getRepository('GeninnoEDSBundle:Nodeaccess');
$options = $repository->createQueryBuilder('na')
.......
->getQuery()
->getResult();
$form = $this->createFormBuilder()
->add('user', 'text', array(
'attr' => array(
'placeholder' => '+ Gebruiker'
)
))
->add('node', 'hidden')
->getForm();
if ($this->getRequest()->isMethod('POST')) {
$form->bind($this->getRequest());
if ($form->isValid()) {
$data = $form->getData();
$user_repository = $this->getDoctrine()
->getRepository('GeninnoEDSBundle:User');
$user = $user_repository->findOneBy(array('username' => $data['user']));
$node_repository = $this->getDoctrine()
->getRepository('GeninnoEDSBundle:Node');
$node = $node_repository->find($data['node']);
$nodeaccess = new Nodeaccess();
$nodeaccess->setAccesslevel(0);
$nodeaccess->setDraw(0);
$nodeaccess->setUserid($user);
$nodeaccess->setNodename($this->getUser()->getUsername() . ' Node');
$nodeaccess->setMac($node);
$em = $this->getDoctrine()->getManager();
$em->persist($nodeaccess);
$em->flush();
}
}
return array('options' => $options, 'form' => $form->createView());
}