Doctrine2 One-To-Many / Many-To-One relations

后端 未结 2 617
傲寒
傲寒 2021-02-03 13:51

So, 1:M / M:1 relations don\'t work the way M:M relations work (obviously), but I thought that with proper configuration, you can get the same output as a M:M relation.

2条回答
  •  梦如初夏
    2021-02-03 14:14

    I figured it out. Hopefully this post can help others who got as frustrated as I did!

    Path.php

    PathOffer.php

    Offer.php

    And for my frontend logic:

    PathController.php

    _getObject('Path', $id); // this function either generates a new entity or grabs one from database depending on $id
    
            $form = $this->get('form.factory')->create(new Form\PathType(), $path);
            $formHandler = $this->get('form.handler')->create(new Form\PathHandler(), $form);
    
            // process form
            if ($formHandler->process()) {
                $this->get('session')->setFlash('notice', 'Successfully ' . ($this->_isEdit($path) ? 'edited' : 'added') . ' path!');
                return $this->redirect($this->generateUrl('admin_path'));
            }
    
            return array(
                'path' => $path,
                'form' => $form->createView(),
                'postUrl' => !$this->_isEdit($path) ? $this->generateUrl('admin_path') : $this->generateUrl('admin_path_edit', array('id' => $path->getId())),
                'paths' => $this->_paginate('Path'),
                'edit' => $this->_isEdit($path) ? true : false
            );
        }
    
        [...]
    

    PathType.php (the path form)

    add('name')
                ->add('title')
                ->add('offers', 'collection', array(
                    'type' => new PathOfferType(),
                    'allow_add' => true,
                    'allow_delete' => true
                ))
                ->add('active');
        }
    
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'JStout\MainBundle\Entity\Path'
            );
        }
    }
    

    PathOfferType.php (PathType's offers collection type)

    add('offer', 'entity', array(
                    'class' => 'JStout\MainBundle\Entity\Offer',
                    'query_builder' => function($repository) { return $repository->createQueryBuilder('o')->orderBy('o.name', 'ASC'); },
                    'property' => 'name'
                )) 
                ->add('pos', 'integer');
        }
    
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'JStout\MainBundle\Entity\PathOffer'
            );
        }
    }
    

    PathHandler.php (how I process the form)

    form = $form;
            $this->request = $request;
            $this->entityManager = $entityManager;
        }
    
        public function process()
        {
            if ('POST' == $this->request->getMethod()) {
                // bind form data
                $this->form->bindRequest($this->request);
    
                // If form is valid
                if ($this->form->isValid() && ($path = $this->form->getData()) instanceOf Path) {
                    // save offer to the database
                    $this->entityManager->persist($path);
    
                    foreach ($path->getOffers() as $offer) {
                        $offer->setPath($path);
                        $this->entityManager->persist($offer);
                    }
    
                    $this->entityManager->flush();
    
                    return true;
                }
            }
    
            return false;
        }
    }
    

提交回复
热议问题