我使用 Easyadmin bundle 作为后端来管理所有实体,所以我创建了 2 个实体Job和Category并且它们之间的关系是多对多的
之后,我在工作实体一侧尝试测试,填写字段并选择一个类别并提交 - 一切正常(JobCat 中的新记录包含两个实体 id)
问题是当我开始更新工作实体时,我收到此错误:
使用参数 [12, 3] 执行“INSERT INTO JobCat (id_job, id_category) VALUES (?, ?)”时发生异常:
SQLSTATE [23000]:完整性约束违规:1062 键 'PRIMARY' 的重复条目 '12-3'
这是我的两个实体:
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="label", type="string", length=255)
*/
private $label;
/**
* @var string
*@ORM\ManyToMany(targetEntity="Job", mappedBy="categories", cascade={"persist"})
*/
private $jobs ;
/**
* Constructor
*/
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set label
*
* @param string $label
*
* @return Category
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* @return string
*/
public function getLabel()
{
return $this->label;
}
/**
* Add job
*
* @param \AppBundle\Entity\Job $job
*
* @return Category
*/
public function addJob(\AppBundle\Entity\Job $job)
{
$this->jobs[] = $job;
return $this;
}
/**
* Remove job
*
* @param \AppBundle\Entity\Job $job
*/
public function removeJob(\AppBundle\Entity\Job $job)
{
$this->jobs->removeElement($job);
}
/**
* Get jobs
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getJobs()
{
return $this->jobs;
}
public function __toString() {
return $this->label ;
}
}
工作实体:
/**
* Job
*
* @ORM\Table(name="job")
* @ORM\Entity(repositoryClass="AppBundle\Repository\JobRepository")
* @Vich\Uploadable
*/
class Job
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=300)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=1000)
*/
private $description;
/**
* @var float
*
* @ORM\Column(name="price", type="float")
*/
private $price;
// added for image type ** lool
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $image;
/**
* @Vich\UploadableField(mapping="product_images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @var ArrayCollection categories $categories
* Owning Side
*
* @ORM\ManyToMany(targetEntity="Category", inversedBy="jobs" , cascade={"persist"})
* @ORM\JoinTable(name="JobCat",
* joinColumns={@ORM\JoinColumn(name="id_job", referencedColumnName="id") },
* inverseJoinColumns={@ORM\JoinColumn(name="id_category", referencedColumnName="id")}
* )
*/
private $categories ;
/**
* Constructor
*/
public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
// var_dump($this) ; die ;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Job
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
*
* @return Job
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set price
*
* @param float $price
*
* @return Job
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Set image
*
* @param string $image
*
* @return Job
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
// image file not pushed to the dsatabase
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// do what u want too .
}
}
public function getImageFile()
{
return $this->imageFile;
}
/**
* Add category
*
* @param \AppBundle\Entity\Category $category
*
* @return Job
*/
/* public function addCategory(\AppBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
} */
/**
* Remove category
*
* @param \AppBundle\Entity\Category $category
*/
public function removeCategory(\AppBundle\Entity\Category $category)
{
$this->categories->removeElement($category);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
public function __toString() {
return $this->title ;
}
// added after many
public function setCategories(\AppBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
}
请问有人可以帮我解决吗