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();