问题描述
我试图在数据库中插入一个新的对象/行。对象是这样创建的:
$ nodeaccess = new Nodeaccess(); // A由doctrine2生成的实体
$ nodeaccess-> setAccesslevel(0);
$ nodeaccess-> setDraw(0);
$ nodeaccess-> setUserid($ userid);
$ nodeaccess-> setNodename($ this-> getUser() - > getUsername()。'Node');
$ nodeaccess-> setMac($ node);
表的所有列已设置。当我打印 $ nodeaccess-> getUserid()
和 $ nodeaccess-> getMac()
所需的结果是打印。而且它们是都不为空。
但是当对象持久化时,如下所示:
$ em = $ this-> getDoctrine() - > getManager();
$ em-> persist($ nodeaccess);
$ em-> flush();
发生以下错误:
组合的mac和userID是主键,并且它们都是外键。
他们在模型中设置如下:
/ **
* @var integer
*
* @ ORM\Column(name =mac,type =bigint,nullable = false)
* @ ORM\Id
* /
私人$ mac;
/ **
* @var整数
*
* @ ORM\Column(name =userID,type =integer,nullable = false )
* @ ORM\Id
** /
private $ userid;
公共访问器已实现,我尝试将字段更改为public,但没有帮助。 p>
更新
访问者:
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整数
*
* @ ORM\Column(name =id,type =integer,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY )
* /
private $ id;
/ **
* @var整数
*
* @ ORM\Column(name =mac,type =bigint,nullable = false )
* /
private $ mac;
/ **
* @var整数
*
* @ 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();
}
}
返回数组('options'=> $ options,'form'=> $ form-> createView());
}
重新生成这个特定的实体(哦哦)。字段已更改为:
/ **
* @var \User
*
* @ ORM\ManyToOne(targetEntity =User)
* @ ORM\JoinColumns({
* @ ORM\JoinColumn(name =userID,referencedColumnName =id)
*})
* /
private $ userid;
/ **
* @var \Node
*
* @ ORM\ManyToOne(targetEntity =Node)
* @ORM \JoinColumns({
* @ ORM\JoinColumn(name =mac,referencedColumnName =mac)
*})
* /
private $ mac;
更新
/ **
* @var \Doctrine\Common\Collections\Collection
*
* @ ORM\ManyToOne(targetEntity =User,inversedBy =accessNodes)
* @ ORM\JoinColumn(name =userID,referencedColumnName =id)
** /
private $ accessUsers;
I am trying to insert a new object/row in a database. The object is created like so:
$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);
All columns of the table are set. When I print $nodeaccess->getUserid()
and $nodeaccess->getMac()
the desired results are printed. And they are both not null.
But when the object is persisted like so:
$em = $this->getDoctrine()->getManager();
$em->persist($nodeaccess);
$em->flush();
the following error happens:
The mac and userID combined is the primary key and they are both foreign keys aswell.They are setup in the model like this:
/**
* @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 accessors are implemented and I have tried changing the fields to public but it did not help.
UpdateThe accessors:
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;
}
Update 2I have changed the table, now only the mac
field is NULL.The new model:
/**
* @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;
UPDATE The controller action:
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());
}
The problem has been fixed by regenerating this particular entity (oh oh). The fields have changed to:
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userID", referencedColumnName="id")
* })
*/
private $userid;
/**
* @var \Node
*
* @ORM\ManyToOne(targetEntity="Node")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="mac", referencedColumnName="mac")
* })
*/
private $mac;
UPDATE
That might not have been the fix I thought it was. Had the problem again after adding some fields back to the class. I found out that by removing the following code the user was no longer assigned to null while persisting.
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="accessNodes")
* @ORM\JoinColumn(name="userID", referencedColumnName="id")
**/
private $accessUsers;
这篇关于Doctrine2 - 在冲洗之前,列更改为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!