-1

I have two tables named jobs and attachments.A job may or may not have one or more than one attachments.I have created one to may relation with job and attachment.But when I trying to persist it gives me an error,

A new entity was found through the relationship 'AppBundle\Entity\JotJobs#attachments' that was not configured to cascade persist operations for entity: AppBundle\Entity\JotJobAttachments@000000004d40cceb00000000fe114bdc. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\JotJobAttachments#__toString()' to get a clue. 

Then I have tried to set cascade persist in jobs entity, after that it always asking for a mandatory attachment for each jobs.Otherwise it will gives an error with job_id can't be null in attachment table.I were trying to correct it for the last few hours.Please help.

My entities are,

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * JotJobs
 *
 * @ORM\Table(name="jot_jobs")
 * @ORM\Entity
 */
class JotJobs
{
   /**
     * @var \JotJobAttachments
     *
     * @ORM\OneToMany(targetEntity="JotJobAttachments" ,mappedBy="jotJobs")
     * @ORM\JoinColumn(name="ID", referencedColumnName="job_id")
     */
     private $attachments;

     /**
     * Constructor
     */
    public function __construct()
    {
        $this->attachments = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Add attachments
     *
     * @param \AppBundle\Entity\JotJobAttachments $attachments
     * @return JotJobs
     */
    public function addAttachment(\AppBundle\Entity\JotJobAttachments $attachments=null)
    {
        $this->attachments[] = $attachments;
        return $this;
    }

    /**
     * Remove attachments
     *
     * @param \AppBundle\Entity\JotJobAttachments $attachments
     */
    public function removeAttachment(\AppBundle\Entity\JotJobAttachments $attachments)
    {
        $this->attachments->removeElement($attachments);
    }
}


<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * JotJobAttachments
 *
 * @ORM\Table(name="jot_job_attachments")
 * @ORM\Entity
 */
class JotJobAttachments
{
    /**
     * @var \JotJobs
     *
     * @ORM\ManyToOne(targetEntity="JotJobs", inversedBy="attachments")
     * @ORM\JoinColumn(name="job_id", referencedColumnName="ID", nullable=true)
     */
     private $jotJobs;

      /**
     * Set jotJobs
     *
     * @param \AppBundle\Entity\JotJobs $jotJobs
     * @return JotJobAttachments
     */
    public function setJotJobs(\AppBundle\Entity\JotJobs $jotJobs = null)
    {
        $this->jotJobs = $jotJobs;

        return $this;
    }

    /**
     * Get jotJobs
     *
     * @return \AppBundle\Entity\JotJobs 
     */
    public function getJotJobs()
    {
        return $this->jotJobs;
    }
}

In my controller,

$newJob = new JotJobs();
$newJob->setJobName($data->getJobName());
.
.
.
$attachments = $data->getAttachments();
            $jobDir = $this->container->getParameter('uploads_directory').'/jobs';
            foreach ($attachments as $key => $value) {
                if($value->getAttachment()!=null)
                {
                    /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
                    $file = $value->getAttachment();

                    $fileName = md5(uniqid()).'.'.$file->guessExtension();
                    $file->move($jobDir, $fileName);

                    $jobAttachment = new JotJobAttachments();
                    $jobAttachment->setAttachment($fileName);
                    $jobAttachment->setAttachmentName($file->getClientOriginalName());
                    $newJob->addAttachment($jobAttachment);
                }
            }
            $entityManager->persist($newJob);  
            $entityManager->flush();
            $lId = $newJob->getId();
user3107066
  • 97
  • 1
  • 10
  • 1
    Possible duplicate of [Doctrine 2: Saving Entity in Complex Relationship](http://stackoverflow.com/questions/7319370/doctrine-2-saving-entity-in-complex-relationship) – kba Nov 11 '15 at 08:32
  • Try [to google](https://www.google.se/webhp#q=stackoverflow+A+new+entity+was+found+through+the+relationship) before you ask a question – Wilt Nov 11 '15 at 08:51

1 Answers1

3

You have two things going on here.

The first, as mentioned before, is that you need cascade={"all"} on your OneToMany relation. Use all instead of persist snce if you delete a job you almost certainly want the attachments to be deleted as well.

The second is that you need to set the job reference in your attachment. That is why you getting those null errors.

public function addAttachment(\AppBundle\Entity\JotJobAttachment $attachment=null)
{
    $this->attachments[] = $attachment;
    $attachment->setJotJob($this); // ADD THIS
    return $this;
}

You might also consider changing thing like JotJobAttachments to JotJobAttachment. Makes your code easier to understand.

And don't pay much attention to the down voters. This cross referencing requirement catches many developers and is not easy to search for.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • This should be the answer. It's also in the docs, [here](http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-tags-to-be-removed) just above this hyperlink. – George Irimiciuc Mar 22 '16 at 14:22
  • Perhaps but drive by questions and down votes are just a fact of stackoverflow life. – Cerad Mar 22 '16 at 14:28