当前位置:  开发笔记 > 编程语言 > 正文

Symfony 3.0嵌套实体不保存

如何解决《Symfony3.0嵌套实体不保存》经验,为你挑选了1个好方法。

所以我有一个实验实体,它有许多RNASeq实体.但是,当我尝试使用RNASeq条目(通过newAction)保存实验时,只保存实验部分.

我的控制器如下:

    getDoctrine()->getRepository('AppBundle:Experiment');
            $experiments = $repository->findAll();
            return $this->render('experiment/index.html.twig',['experiments' => $experiments]);
        }

        /**
         * @Route("/experiment/new", name="experiment_new")
         */
        public function newAction(Request $request) {
            $experiment = new Experiment();

            $form = $this->createForm(ExperimentType::class, $experiment);

            $form->handleRequest($request);

            // On submission.
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($experiment);
                // For each nested object. 
                foreach($experiment->getRNASeqs() as $rnaseq) {
                    $em->persist($rnaseq);
                }
                $em->flush();
                return $this->redirectToRoute('experiment_index');
            }

            return $this->render('experiment/form.html.twig', array('form' => $form->createView()));
        }

        /**
         * @Route("/experiment/show/{id}", name="experiment_show")
         * TODO: Not sure if nessesary (wasn't in last system).
         */
        public function showAction($id) {
            return $this->render('experiment/show.html.twig');
        }

        /**
         * @Route("/experiment/edit/{id}", name="experiment_edit")
         */
        public function editAction(Request $request, $id) {
            $repository = $this->getDoctrine()->getRepository('AppBundle:Experiment');
            $experiment = $repository->find($id);

            $form = $this->createForm(ExperimentType::class, $experiment);

            $form->handleRequest($request);

            return $this->render('experiment/form.html.twig', array('form' => $form->createView()));
        }

}
  ?>

我试图遵循Symfony文档.但是,它并没有特别考虑一对多的例子,我担心这可能是我搞砸了的地方.

为简洁起见,可以在此处找到我的Entity类和其余代码.

编辑:

我注意到在创建事件期间,insert into事件似乎没有正确的关系属性:

    INSERT INTO experiment (title, exp_type, sample_nums) VALUES (?, ?, ?)
Parameters: { 1: fe, 2: RNASeq, 3: 'a:1:{i:0;i:68767;}' }

    INSERT INTO rnaseq (quality, ribodepleted, final_quality, sample_num, protocol_used, step1, step1result, service_provider, platform, data_files, pipeline, pipeline_parameters, result_files, experiment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters: { 1: fds, 2: 0, 3: fes, 4: 7876, 5: esf, 6: 0, 7: fsd, 8: fs, 9: fs, 10: fsd, 11: sdf, 12: fds, 13: fsd, 14: null }

Experiment似乎缺少对其rnaSeqs属性的更新,并且RNASeq有一个null参数experiment_id.



1> Cerad..:

这是一个非常普遍的问题.您需要注意交叉引用对象.尽管许多人最初似乎认为这样做,但是学说并没有自动完成.

class Experiment {
  public function addRNASeq($seq) {
    $this->seqs[] = $seq;
    $seq->setExperiment($this);  // This is what you are missing

并将属性cascade = all添加到Experiment :: seqs属性中.然后,您将不需要显式持久化seq实体.

推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有