Symfony 3.0嵌套实体不保存 [英] Symfony 3.0 nested entities not saving
问题描述
newAction
)保存实验时,只有实验部分保存。 我的控制器如下:
<?php
// src / AppBundle / Controller / ExperimentController.php
命名空间AppBundle\Controller;
使用AppBundle\Entity\Experiment;
使用AppBundle\Entity\RNASeq;
使用AppBundle\Form\Type\ExperimentType;
使用Symfony\Bundle\FrameworkBundle\Controller\Controller;
使用Symfony\Component\HttpFoundation\Request;
使用Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
使用Symfony\Component\HttpFoundation\Response;
// TODO:deleteAction应该被实现。
class ExperimentController extends Controller {
/ **
* @Route(/ experiment / index,name =experiment_index)
* /
public function indexAction(){
//从数据库中抓取所有实验,并将其传递给模板。
$ repository = $ this-> getDoctrine() - > getRepository('AppBundle:Experiment');
$ experiments = $ repository-> findAll();
return $ this-> render('experiment / index.html.twig',['experiment'=> $ experiments]);
}
/ **
* @Route(/ experiment / new,name =experiment_new)
* /
public function newAction (请求$ request){
$ experiment = new Experiment();
$ form = $ this-> createForm(ExperimentType :: class,$ experiment);
$ form-> handleRequest($ request);
//提交。
if($ form-> isValid()){
$ em = $ this-> getDoctrine() - > getManager();
$ em-> persist($ experiment);
//对于每个嵌套对象。
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:不确定如果nessesary(不在最后的系统)。
* /
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文档。但是,这并不是一个一对多的例子,我担心这可能是我搞砸了。
为了简洁起见,我的实体类和其余代码可以找到 here 。
编辑:
我注意到,在创建活动期间,插入到事件中的似乎没有正确的关系属性:
INSERT INTO实验(标题,exp_type,sample_nums)VALUES(?,?,?)
参数:{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参数:{1:fds,2:...,p,...,..., 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}
实验
似乎缺少对其 rnaSeqs
属性的更新,而 RNASeq
null
参数 experiment_id
。
这是一个很常见的问题。你需要照顾交叉引用你的对象。原则不会自动执行,尽管许多人最初似乎认为会这样。
class Experiment {
public function addRNASeq ($ seq){
$ this-> seqs [] = $ seq;
$ seq-> setExperiment($ this); //这是你失踪的
并将属性cascade = all添加到您的Experiment :: seqs属性。那么你不需要显式地保留seq实体。
So I have an Experiment Entity which has many RNASeq entities. However, when I try to save an Experiment with an RNASeq entry (via the newAction
), only the Experiment portion saves.
My controller is as follows:
<?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()));
}
}
?>
I've attempted to follow the Symfony documentation. However, it doesn't specifically go over a One-to-Many example, and I fear this is possibly where I've messed up.
For the sake of brevity my Entity classes and the rest of my code can be found here.
Edit:
I've noticed that during a creation event, the insert into
events don't seem to have the correct relationship attributes:
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
appears to be missing an update to its rnaSeqs
attribute, and RNASeq
has a null
parameter for experiment_id
.
This is a very common issue. You need to take care of cross referencing your objects. Doctrine does not do this automatically though many people initially seem to think it will.
class Experiment {
public function addRNASeq($seq) {
$this->seqs[] = $seq;
$seq->setExperiment($this); // This is what you are missing
And add the attribute cascade=all to your Experiment::seqs property. Then you won't need to explicitly persist the seq entities.
这篇关于Symfony 3.0嵌套实体不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!