php - Symfony 3.0 nested entities not saving


So I have an Experiment Entity which has many RNASeq entities. However, when I try to save an Experiment with an RNASeq entry (via thenewAction), only the Experiment portion saves.

My controller is as follows:

    // 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);


            // On submission.
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                // For each nested object. 
                foreach($experiment->getRNASeqs() as $rnaseq) {
                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);


            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.


I've noticed that during a creation event, theinsert 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 itsrnaSeqs attribute, andRNASeq has anull parameter forexperiment_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.

People are also looking for solutions to the problem: php - Forloop error codeigniter


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.