An exception occurred while executing 'INSERT INTO JobCat

973 Views Asked by At

I'm using Easyadmin bundle as back-end to manage all entities so i create 2 entities Job and Category and the relation between them is many to many

After that i try the test in the side of the job entity, i fill the fields and i choose one category and submit - everything is ok ( new record in the JobCat contains both entities id )

The problem is when i'm starting to update the job entity i get this Error :

An exception occurred while executing 'INSERT INTO JobCat (id_job, id_category) VALUES (?, ?)' with params [12, 3]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '12-3' for key 'PRIMARY'

Here are my two entites :

    /**
     * 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 ; 
    }


    }

The job entity :

/**
 * 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;
}

}

please Can someone help me to fix it

1

There are 1 best solutions below

1
On

I think you should remove one of the cascade persist.

You are telling Doctrine to save immediately a Category when you save a Job and also do the opposite. So when you save a Job automatically the associated Category is saved. Then Doctrine tries to save the associated Job again but it's already in the database so it fails.

So decide which entity owns the association and modify the cascade behaviour in consequence.

By the way, it will be helpful if you post your persist flush code.