我是初学者,我无法弄清楚如何解决我的问题。
我有一个ManyToOne / OneToMany关联,我在表单类型中添加了一个字段,但handlerequest没有考虑关联,无论是在创建还是在编辑中。
我如何继续保存我的关联?
这是我的视频控制器
<?php
class VideoController extends Controller
{
...
/**
* Creates a new Video entity.
*
* @Route("/new", name="video_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$video = new Video();
$form = $this->createForm('AppBundle\Form\VideoType', $video);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($video);
$em->flush();
return $this->redirectToRoute('video_show', array('id' => $video->getId()));
}
return $this->render('video/new.html.twig', array(
'video' => $video,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing Video entity.
*
* @Route("/{id}/edit", name="video_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Video $video)
{
$deleteForm = $this->createDeleteForm($video);
$editForm = $this->createForm('AppBundle\Form\VideoType', $video);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($video);
$em->flush();
return $this->redirectToRoute('video_edit', array('id' => $video->getId()));
}
return $this->render('video/edit.html.twig', array(
'video' => $video,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
...
}
现在是JT实体
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* JT
*
* @ORM\Table(name="j_t")
* @ORM\Entity(repositoryClass="AppBundle\Repository\JTRepository")
*/
class JT
{
...
/**
* @ORM\OneToMany(targetEntity="Video", mappedBy="jT")
*/
private $videos;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Add videos
*
* @param \AppBundle\Entity\Video $videos
* @return JT
*/
public function addVideo(\AppBundle\Entity\Video $videos)
{
$this->videos[] = $videos;
return $this;
}
/**
* Remove videos
*
* @param \AppBundle\Entity\Video $videos
*/
public function removeVideo(\AppBundle\Entity\Video $videos)
{
$this->videos->removeElement($videos);
}
/**
* Get videos
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getVideos()
{
return $this->videos;
}
}
视频实体
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Video
*
* @ORM\Table(name="video")
* @ORM\Entity(repositoryClass="AppBundle\Repository\VideoRepository")
*/
class Video
{
....
/**
* @ORM\ManyToOne(targetEntity="JT", inversedBy="videos")
* @ORM\JoinColumn(name="jT_id", referencedColumnName="id")
*/
private $jT;
/**
* Set jT
*
* @param \AppBundle\Entity\JT $jT
* @return Video
*/
public function setJT(\AppBundle\Entity\JT $jT = null)
{
$this->jT = $jT;
return $this;
}
/**
* Get jT
*
* @return \AppBundle\Entity\JT
*/
public function getJT()
{
return $this->jT;
}
/**
* toString
* @return string
*/
public function __toString()
{
return $this->getTitle();
}
}
最后,VideoType表格
class VideoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('jT','entity'
,array("label"=>"JT",
'label_attr' => array('class' => 'control-label'),
'class' => 'AppBundle:JT',
"attr"=>array("class"=>"form-control")
)
);
}
我希望能够选择&#34;视频&#34;属于&#34; jT&#34;,以及视频形式中的哪一个。
PS:我怎么能添加一个&#34; none&#34;表格中的实体输入中的选项?
答案 0 :(得分:0)
您有两个选择:
- 明确保存jT实体
$jT = $video->getJT();
$jT->addVideo($video);
$em->persist($jT);
$em->persist($video);
$em->flush();
- 为cascade persist配置实体
/**
* @ORM\ManyToOne(targetEntity="JT", inversedBy="videos", cascade={"all"})
* @ORM\JoinColumn(name="jT_id", referencedColumnName="id")
*/
private $jT;
public function setJT(JT $jT)
{
$this->jT = $jT;
$jT->addVideo($this)
return $this;
}
/**
* @ORM\OneToMany(targetEntity="Video", mappedBy="jT", cascade={"all"})
*/
private $videos;