我是初学者,我不知道如何解决我的问题。
我有一个 ManyToOne/OneToMany 关联,并且在表单类型中添加了一个字段,但句柄请求在创建或编辑中都没有考虑该关联。
如何继续保存我的关联?
这是我的视频控制器
<?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('AppBundleFormVideoType', $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('AppBundleFormVideoType', $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 AppBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
/**
* JT
*
* @ORMTable(name="j_t")
* @ORMEntity(repositoryClass="AppBundleRepositoryJTRepository")
*/
class JT
{
...
/**
* @ORMOneToMany(targetEntity="Video", mappedBy="jT")
*/
private $videos;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Add videos
*
* @param AppBundleEntityVideo $videos
* @return JT
*/
public function addVideo(AppBundleEntityVideo $videos)
{
$this->videos[] = $videos;
return $this;
}
/**
* Remove videos
*
* @param AppBundleEntityVideo $videos
*/
public function removeVideo(AppBundleEntityVideo $videos)
{
$this->videos->removeElement($videos);
}
/**
* Get videos
*
* @return DoctrineCommonCollectionsCollection
*/
public function getVideos()
{
return $this->videos;
}
}
视频实体
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Video
*
* @ORMTable(name="video")
* @ORMEntity(repositoryClass="AppBundleRepositoryVideoRepository")
*/
class Video
{
....
/**
* @ORMManyToOne(targetEntity="JT", inversedBy="videos")
* @ORMJoinColumn(name="jT_id", referencedColumnName="id")
*/
private $jT;
/**
* Set jT
*
* @param AppBundleEntityJT $jT
* @return Video
*/
public function setJT(AppBundleEntityJT $jT = null)
{
$this->jT = $jT;
return $this;
}
/**
* Get jT
*
* @return AppBundleEntityJT
*/
public function getJT()
{
return $this->jT;
}
/**
* toString
* @return string
*/
public function __toString()
{
return $this->getTitle();
}
}
最后,视频类型表单
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")
)
);
}
我希望能够从视频形式中选择"视频"是否属于"jT",以及哪一个。
PS:如何在表单的实体输入中添加"无"选项?
您有 2 个选项:
-- 显式保存 jT 实体
$jT = $video->getJT();
$jT->addVideo($video);
$em->persist($jT);
$em->persist($video);
$em->flush();
-- 为级联持久配置实体
/**
* @ORMManyToOne(targetEntity="JT", inversedBy="videos", cascade={"all"})
* @ORMJoinColumn(name="jT_id", referencedColumnName="id")
*/
private $jT;
public function setJT(JT $jT)
{
$this->jT = $jT;
$jT->addVideo($this)
return $this;
}
/**
* @ORMOneToMany(targetEntity="Video", mappedBy="jT", cascade={"all"})
*/
private $videos;
后者的学说描述:http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/working-with-associations.html#association-management-methods