【问题标题】:Symfony 3.0 nested entities not savingSymfony 3.0 嵌套实体不保存
【发布时间】:2015-12-10 09:08:52
【问题描述】:

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

我的控制器如下:

    <?php
    // src/AppBundle/Controller/ExperimentController.php
    namespace AppBundle\Controller;

    use AppBundle\Entity\Experiment;
    use AppBundle\Entity\RNASeq;
    use AppBundle\Form\Type\ExperimentType;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Component\HttpFoundation\Response;

    // TODO: deleteAction should be implemented. 

    class ExperimentController extends Controller {
        /**
         * @Route("/experiment/index", name="experiment_index")
         */
        public function indexAction() {
            // Grab all experiments from database and hand them to template. 
            $repository = $this->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 documentation。但是,它并没有专门讨论一对多的例子,我担心这可能是我搞砸的地方。

为了简洁起见,我的实体类和我的其余代码可以在here找到。

编辑:

我注意到在创建事件期间,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

【问题讨论】:

  • 你是否进入了 foreach (foreach($experiment->getRNASeqs() as $rnaseq))。尝试输出孩子的数量以确保关系被填充:$experiment->getRNASeqs()->count()
  • 它输出int(1),我认为这意味着我要带孩子? (我用了一个孩子)。我已经通过进入循环并打印一个变量(质量)来确认这一点:string(2) "ef"
  • 只是一个问题:您使用的是哪个版本的 symfony? 3.0 ?
  • 是的,没错。 (更新了标题并更正了标签,感谢现场)。
  • 您似乎忘记了experimentType 中的sampleNums 属性。根据您的实体注释,这不能为空。

标签: php doctrine-orm symfony


【解决方案1】:

这是一个很常见的问题。您需要注意交叉引用您的对象。 Doctrine 不会自动执行此操作,尽管许多人最初似乎认为它会自动执行。

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

并将属性 cascade=all 添加到 Experiment::seqs 属性。然后你就不需要显式地持久化 seq 实体了。

【讨论】:

  • 工作得很好,我会记住未来的对象。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2020-11-28
相关资源
最近更新 更多