SQLSTATE [23000]:完整性约束违规:1048列“X”不能为空

时间:2014-07-08 11:46:33

标签: symfony doctrine entity relational

嗨,我一直在网上搜索,我不明白为什么会收到此错误。我在这段代码中看不到任何错误。 错误是:

An exception occurred while executing 'INSERT INTO gallery (play_id, src) VALUES (?, ?)' with params [null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'play_id' cannot be null

500内部服务器错误 - DBALException 1个链接异常: PDOException

我在gallery实体中有play_id的关系。所以任何画廊都可以与戏剧有关。 简化的图库实体:

<?php

namespace mt\adminBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Gallery
 *
 * @ORM\Table(name="gallery")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="mt\adminBundle\Entity\GalleryRepository")
 */
class Gallery
{
/**
 * @var integer
 *
 * @ORM\Column(name="play_id", type="integer", nullable=false)
 */
private $playId;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;



/**
 * Set playId
 *
 * @param integer $playId
 * @return Gallery
 */
public function setPlayId($playId)
{
    $this->playId = $playId;

    return $this;
}

/**
 * Get playId
 *
 * @return integer 
 */
public function getPlayId()
{
    return $this->playId;
}

/**
 * @ORM\ManyToOne(targetEntity="Plays", inversedBy="galleries")
 * @ORM\JoinColumn(name="play_id", referencedColumnName="id")
 */
protected $play;

/**
 * Set play
 *
 * @param \mt\adminBundle\Entity\Plays $play
 * @return Gallery
 */
public function setPlay(\mt\adminBundle\Entity\Plays $play = null)
{
    $this->play = $play;

    return $this;
}

/**
 * Get play
 *
 * @return \mt\adminBundle\Entity\Plays 
 */
public function getPlay()
{
    return $this->play;
}

}

和游戏实体:

<?php

namespace mt\adminBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Plays
 *
 * @ORM\Table(name="plays")
 * @ORM\Entity(repositoryClass="mt\adminBundle\Entity\PlaysRepository")
 */
class Plays
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * @ORM\OneToMany(targetEntity="Gallery", mappedBy="play")
 */
protected $galleries;

public function __construct()
{
    $this->galleries = new ArrayCollection();
}

/**
 * Add galleries
 *
 * @param \mt\adminBundle\Entity\Gallery $galleries
 * @return Plays
 */
public function addGallery(\mt\adminBundle\Entity\Gallery $galleries)
{
    $this->galleries[] = $galleries;

    return $this;
}

/**
 * Remove galleries
 *
 * @param \mt\adminBundle\Entity\Gallery $galleries
 */
public function removeGallery(\mt\adminBundle\Entity\Gallery $galleries)
{
    $this->galleries->removeElement($galleries);
}

/**
 * Get galleries
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getGalleries()
{
    return $this->galleries;
}

}

我用symfony Doctrine文档编写了这个实体! 我的控制员:

public function addPicAction(){
    $request = Request::createFromGlobals();
    $play = $request->request->get('galleryPlay');

        $gallery = new Gallery();
        $gallery->setPlayId($play);
        $em = $this->getDoctrine()->getManager();
        $em->persist($gallery);
        $em->flush();

    $this->get('session')->getFlashBag()->add('notice', 'success!');
    return $this->redirect($this->generateUrl('galleries'));
}

对这个实体的SELECT查询做得很好但是当我想将一个新的gallery对象持久化到db时我得到了错误!有什么机构可以帮助我吗?

0 个答案:

没有答案