Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:38:18+00:00 2026-05-23T10:38:18+00:00

So, 1:M / M:1 relations don’t work the way M:M relations work (obviously), but

  • 0

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.

Basically, I needed to add another field (position), to path_offer.

I thought I got it to work until I tried to use $path->getOffers() which returned a PersistentCollection instead of what I thought is forced (an ArrayCollection of Offers). Anyway, inside the current table, I have two entries: two offers to one path. $path->getOffers() is returning a PersistantCollection of a PathOffer which only has one Offer attatched and not both.

My question is how to really use these types of relations? Because I need it with many other aspects of this project I’m working on (many M:M collections also need to be positioned)

My code is below!

Path.php

[..]

/**
 * @ORM\Entity
 * @ORM\Table(name="path")
 */
class Path
{
    /**
     * @var integer
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="PathOffer", mappedBy="offer", cascade={"all"})
     */
    protected $offers;

[..]

PathOffer.php

[..]

/**
 * @ORM\Entity
 * @ORM\Table(name="path_offer")
 */
class PathOffer
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Path", inversedBy="offers", cascade={"all"})
     */
    protected $path;

    /**
     * @ORM\ManyToOne(targetEntity="Offer", inversedBy="offers", cascade={"all"})
     */
    protected $offer;

    /**
     * @ORM\Column(type="integer")
     */
    protected $pos;

[..]

Offer.php

[..]

/**
 * @ORM\Entity
 * @ORM\Table(name="offer")
 */
class Offer
{
    /**
     * @var integer
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var \ZGoffers\MainBundle\Entity\PathOffer
     *
     * @ORM\OneToMany(targetEntity="PathOffer", mappedBy="path", cascade={"all"})
     */
    protected $paths;

[..]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:38:19+00:00Added an answer on May 23, 2026 at 10:38 am

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

    Path.php

    <?php
    
    namespace JStout\MainBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="path")
     */
    class Path
    {
        /**
         * @var integer
         *
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var \JStout\MainBundle\Entity\PathOffer
         *
         * @ORM\OneToMany(targetEntity="PathOffer", mappedBy="path", cascade={"all"})
         * @ORM\OrderBy({"pos" = "ASC"})
         */
        private $offers;
    
        [...]
    

    PathOffer.php

    <?php
    
    namespace JStout\MainBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="path_offer")
     */
    class PathOffer
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="Path", inversedBy="offers", cascade={"all"})
         */
        private $path;
    
        /**
         * @ORM\ManyToOne(targetEntity="Offer", inversedBy="paths", cascade={"all"})
         */
        private $offer;
    
        /**
         * @ORM\Column(type="integer")
         */
        private $pos;
    
        [...]
    

    Offer.php

    <?php
    
    namespace JStout\MainBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="offer")
     */
    class Offer
    {
        /**
         * @var integer
         *
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var \JStout\MainBundle\Entity\PathOffer
         *
         * @ORM\OneToMany(targetEntity="PathOffer", mappedBy="offer", cascade={"all"})
         */
        private $paths;
    
        [...]
    

    And for my frontend logic:

    PathController.php

    <?php
    
        [...]
    
        /**
         * @Extra\Route("/path", name="admin_path")
         * @Extra\Route("/path/{id}/edit", name="admin_path_edit", requirements={"id" = "\d+"})
         * @Extra\Template()
         */
        public function pathAction($id = null)
        {
            $path = $this->_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)

    <?php
    
    namespace JStout\MainBundle\Form;
    
    use Symfony\Component\Form\AbstractType,
        Symfony\Component\Form\FormBuilder;
    
    class PathType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->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)

    <?php
    
    namespace JStout\MainBundle\Form;
    
    use Symfony\Component\Form\AbstractType,
        Symfony\Component\Form\FormBuilder;
    
    class PathOfferType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->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)

    <?php
    
    namespace JStout\MainBundle\Form;
    
    use JStout\MainBundle\Component\Form\FormHandlerInterface,
        Symfony\Component\Form\Form,
        Symfony\Component\HttpFoundation\Request,
        Doctrine\ORM\EntityManager,
        JStout\MainBundle\Entity\Path;
    
    class PathHandler implements FormHandlerInterface
    {
        protected $form;
        protected $request;
        protected $entityManager;
    
        public function buildFormHandler(Form $form, Request $request, EntityManager $entityManager)
        {
            $this->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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sorry, but I don't really get doctrine. I thought I got but
I searched a lot but could not find a way to dump table relations
I'm trying to learn to work with Hibernate but probably i don't understand @ManyToOne
I don't see the relations view that used to exist. Any suggestions? I'd rather
One-to-one relations within nhibernate can be lazyloaded either false or proxy. I was wondering
My user model has three relations for the same message model, and is using
Imagine you have an entity that has some relations with other entities and you
I searched for a long time, but I don't manage to retrieve two related
I’m looking for a script that allows me to define my database relations in
I'm trying to get a handle on DDD and feel that I've got a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.