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 8780519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:04:22+00:00 2026-06-13T20:04:22+00:00

I have a M2M relationship, but when I create an item the relationship is

  • 0

I have a M2M relationship, but when I create an item the relationship is not saved and I can’t find where’s the problem.
The Model:

class Serie
 {    

/**
 * @ORM\ManyToMany(targetEntity="Magazine", mappedBy="series")
 * */
protected $magazines;

/**
 * Constructor
 */
public function __construct()
{
    $this->magazines = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add magazines
 *
 * @param MyList\DBBundle\Entity\Magazine $magazines
 * @return Serie
 */
public function addMagazine(\MyList\DBBundle\Entity\Magazine $magazines)
{
    $this->magazines[] = $magazines;

    return $this;
}

/**
 * Remove magazines
 *
 * @param MyList\DBBundle\Entity\Magazine $magazines
 */
public function removeMagazine(\MyList\DBBundle\Entity\Magazine $magazines)
{
    $this->magazines->removeElement($magazines);
}

/**
 * Get magazines
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getMagazines()
{
    return $this->magazines;
}
}

The Magazine class:

class Magazine
{

/**
 * @ORM\ManyToMany(targetEntity="Serie" , inversedBy="magazines")
 * @ORM\JoinTable(name="magazines_series")
 * */
protected $series;

public function __construct()
{
    $this->series = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add series
 *
 * @param MyList\DBBundle\Entity\Serie $series
 * @return Magazine
 */
public function addSerie(\MyList\DBBundle\Entity\Serie $serie)
{
    $serie->addMagazine($this);
    $this->series[] = $serie;

    return $this;
}

/**
 * Remove series
 *
 * @param MyList\DBBundle\Entity\Serie $series
 */
public function removeSerie(\MyList\DBBundle\Entity\Serie $series)
{
    $this->series->removeElement($series);
}

/**
 * Get series
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getSeries()
{
    return $this->series;
}
}

The Controller
class SerieController extends Controller
{
public function newAction()
{
$entity = new Serie();
$form = $this->createForm(new SerieType(), $entity);

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a new Serie entity.
 *
 * @Route("/create", name="serie_create")
 * @Method("POST")
 * @Template("DBBundle:Serie:new.html.twig")
 */
public function createAction(Request $request)
{
    $entity  = new Serie();
    $form = $this->createForm(new SerieType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('serie_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}
}

And the Form:

class SerieType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('start')
        ->add('end')
        ->add('type')
        ->add('status')
        ->add('magazines','entity',array(
                'class' => 'DBBundle:Magazine',
                'multiple' => true,
                'property' => 'name'
                ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MyList\DBBundle\Entity\Serie'
    ));
}

public function getName()
{
    return 'mylist_dbbundle_serietype';
}
}

I know there are some questions about this (i.e. Symfony2-Doctrine: ManyToMany relation is not saved to database), but none of them have solved my problem.

  • 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-06-13T20:04:23+00:00Added an answer on June 13, 2026 at 8:04 pm

    This is because you never persist() the magazine, the form added it already to the entity but Doctrine still needs to manage it.

    add this in the if ($form->isValid()):

    ...
    $magazines = $entity->getMagazines();
    foreach($magazines as $magazine){
        $em->persist($magazine);
    }
    ...
    $em->flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a model using m2m feature: class Classroom(models.Model): user = models.ForeignKey(User, related_name =
I have created the following M2M table with an intermediary model -- class Position(models.Model):
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have a recursive M2M relationship with an indicator class. An indicator is a
I have a model with a m2m relation to users: class SomeModel(models.Model): user=models.ManyToManyField(User,related_name='linked',blank=True,null=True) to
I have a M2M field in my django model project. In my view I
If I have a Pizza model and a Topping model, with m2m between them,
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have a model models.py : class MyModelClass(models.Model): name = models.CharField(max_length=255) m2m_1 = models.ManyToManyField(A,
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,

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.