执行'INSERT INTO JobCat时发生异常

时间:2017-12-10 18:29:07

标签: php symfony symfony2-easyadmin

我使用Easyadmin捆绑包作为后端管理所有实体,因此我创建了2个实体作业类别,它们之间的关系是多对多

之后我在作业实体一侧尝试测试,我填写字段,我选择一个类别并提交 - 一切正常(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;
}

}

请有人帮我修理它

1 个答案:

答案 0 :(得分:0)

我认为你应该删除其中一个级联持久化。

您正在告诉Doctrine在您保存作业时立即保存类别,并执行相反的操作。因此,当您自动保存作业时,将保存关联的类别。然后Doctrine尝试再次保存关联的Job,但它已经在数据库中,因此它失败了。

因此,决定哪个实体拥有该关联,并因此修改级联行为。

顺便说一下,如果你发布持久的刷新代码会很有帮助。