我无法使用doctrain2和ZF2中的oneTomany关系将数据保存到表中。
它给出了MySQL外键空错误。
An exception occurred while executing 'INSERT INTO phv_image (path, name, suggestion_id) VALUES (?, ?, ?)' with params ["test.png", "test", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'suggestion_id' cannot be null
我的代码是
1个实体
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* PhvImage
*
* @ORM\Table(name="phv_image", indexes={@ORM\Index(name="suggestion_id", columns={"suggestion_id"})})
* @ORM\Entity
*/
class PhvImage
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=500, nullable=false)
*/
private $path;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* @var \PhvSuggestion
*
* @ORM\ManyToOne(targetEntity="PhvSuggestion", inversedBy="images")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="suggestion_id", referencedColumnName="id")
* })
*/
private $suggestion;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
*
* @return PhvImage
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set name
*
* @param string $name
*
* @return PhvImage
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set suggestion
*
* @param \PhvSuggestion $suggestion
*
* @return PhvImage
*/
public function setSuggestion(\Application\Entity\PhvSuggestion $suggestion = null)
{
$this->suggestion = $suggestion;
return $this;
}
/**
* Get suggestion
*
* @return \PhvSuggestion
*/
public function getSuggestion()
{
return $this->suggestion;
}
}
2实体
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* PhvSuggestion
*
* @ORM\Table(name="phv_suggestion", indexes={@ORM\Index(name="user_id", columns={"user_id"})})
* @ORM\Entity
*/
class PhvSuggestion extends Entity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
public $id;
/**
* @var string
*
* @ORM\Column(name="subject", type="string", length=500, nullable=false)
*/
private $subject;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=false)
*/
private $description;
/**
* @var \PhvUser
*
* @ORM\ManyToOne(targetEntity="PhvUser")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="PhvImage", mappedBy="suggestion", cascade={"persist"})
*/
public $images;
private $data = null;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set subject
*
* @param string $subject
*
* @return PhvSuggestion
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Get subject
*
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* Set description
*
* @param string $description
*
* @return PhvSuggestion
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set user
*
* @param \PhvUser $user
*
* @return PhvSuggestion
*/
public function setUser(PhvUser $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \PhvUser
*/
public function getUser()
{
return $this->user;
}
public function setImages($images){
$this->images = $images;
return $this;
}
public function getImages() {
return $this->images;
}
}
控制器
public function saveAction() {
$data = $this->getRequest()->getPost()->toArray();
$imgObj = new \Application\Entity\PhvImage();
$imgObj->setPath('test.png');
$imgObj->setName('test');
$imgObj2 = new \Application\Entity\PhvImage();
$imgObj2->setPath('new.png');
$imgObj2->setName('new');
$arr = array(
$imgObj,$imgObj2
);
$sugg = new \Application\Entity\PhvSuggestion();
$sugg->setUser(1);
$sugg->setImages($arr);
$sugg->setDescription('test');
$sugg->setSubject('sub');
$em->persist($sugg);
$em->flush();
}
你能帮我解决这个问题。
答案 0 :(得分:0)
首先在类PhvSuggestion
中,您需要添加一个构造函数,如下所示:
public function __construct()
{
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
}
然后将setImages
更改为:
public function addImages(\Doctrine\Common\Collections\Collection $images)
{
foreach ($images as $image) {
$image->setSuggestion($this);
$this->images->add($image);
}
}
您可以在Doctrine 2文档中找到所有这些详细信息。