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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀