duan1443
2013-04-19 08:41
浏览 48
已采纳

Doctrine2 - 在刷新之前,列变为null

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());
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douliao5467 2013-04-26 06:52
    已采纳

    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;
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题