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:
An exception occurred while executing 'INSERT INTO nodeaccess (mac, userID, accessLevel, nodeName, draw) VALUES (?, ?, ?, ?, ?)' with params {"1":null,"2":null,"3":0,"4":"Example Node","5":0}:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'mac' cannot be null
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.
Update The 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 2
I 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());
}