我有两个名为jobs和attachments的表。一个作业可能有也可能没有一个或多个附件。我创建了一个可能与作业和附件的关系。但是当我试图坚持它时给了我一个错误,
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.
然后我试图在作业实体中设置级联持久化,之后它总是要求每个作业的强制附件。否则它会给出一个错误,job_id在附件表中不能为空。我正在尝试在过去几个小时内纠正它。请帮助。
我的实体是,
<?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;
}
}
在我的控制器中,
$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();
答案 0 :(得分:3)
这里有两件事。
如前所述,第一个是你需要在OneToMany关系上使用cascade = {&#34; all&#34;}。如果删除作业,请使用all而不是persist snce,您几乎肯定也希望删除附件。
第二,您需要在附件中设置作业参考。这就是你得到那些空错误的原因。
public function addAttachment(\AppBundle\Entity\JotJobAttachment $attachment=null)
{
$this->attachments[] = $attachment;
$attachment->setJotJob($this); // ADD THIS
return $this;
}
您也可以考虑将JotJobAttachments之类的内容更改为JotJobAttachment。使您的代码更易于理解。
并且不要太关注失败的选民。这种交叉引用要求会吸引许多开发人员,并且不容易搜索。