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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:19:47+00:00 2026-06-10T11:19:47+00:00

I don’t know why, maybe i am missing some basic logic but I always

  • 0

I don’t know why, maybe i am missing some basic logic but I always run again into the same issue. I can’t persists ManyToMany collections, and it also faces me with OneToMany collections, though I can work around that.

I read through the doctrine documentation, and I think I do understand the thing with mappedBy and inversedBy (where the last one is always the owner and therefor responsible for persisting the data, please correct me if I am wrong).

So here’s a basic example that I have right now, which I can’t figure out.

I have an Entity called Site:

#Site.php
...
/**
 * @ORM\ManyToMany(targetEntity="Category", mappedBy="sites")
 */
protected $categories;

and another one called Category:

#Category.php
...
/**
 * @ORM\ManyToMany(targetEntity="Site", inversedBy="categories")
 * @ORM\JoinTable(name="sites_categories")
 */
protected $sites;

Using the Symfony2 entity genenerator it added me some getters and setters to my Entites which look like this.

Site:

#Site.php
...
/**
 * Add categories
 *
 * @param My\MyBundle\Entity\Category $categories
 */
public function addCategory(\My\MyBundle\Entity\Category $categories)
{
    $this->categories[] = $categories;
}

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

The same counts for

Category:

#Category.php
...
/**
 * Add sites
 *
 * @param My\MyBundle\Entity\Site $sites
 */
public function addSite(\My\MyBundle\Entity\Site $sites)
{
    $this->sites[] = $sites;
}

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

Fair enough.

Now in my controller, I am trying to persist a Site object:

public function newsiteAction() {

    $site = new Site();
    $form = $this->createFormBuilder($site); // generated with the FormBuilder, so the form includes Category Entity

    // ... some more logic, like if(POST), bindRequest() etc.

        if ($form->isValid()) {
            $em = $this->getDoctrine()
            ->getEntityManager();
            $em->persist($site);
            $em->flush();
        }
}

The result is always the same. It persists the Site Object, but not the Category entity. And I also know why (I think): Because the Category entity is the owning side.

But, do I always have to do something like this for persisting it? (which is actually my workaround for some OneToMany collections)

$categories = $form->get('categories')->getData();
foreach($categories as $category) {
    // persist etc.
}

But I am running into many issues here, like I would have to do the same loop as above for deleting, editing etc.

Any hints? I will really give a cyber hug to the person who can clear my mind about that. Thanks!

.
.
.

UPDATE

I ended up changing around the relationship (owning and inverse side) between the ManyToMany mapping.

If somebody else runs into that problem, you need to be clear about the concept of bidrectional relationships, which took me a while to understand too (and I hope I got it now, see this link).
Basically what anserwed my question is: The object you want to persist must always be the owning site (The owning site is always the entity that has “inversed by” in the annotiation).

Also there is a concept of cascade annotation (see this link, thanks to moonwave99)

So thanks, and I hope that helps somebody for future reference! 🙂

  • 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-10T11:19:49+00:00Added an answer on June 10, 2026 at 11:19 am

    Regarding OneToMany relationship, you want to know about cascade annotation – from Doctrine docs [8.6]:

    The following cascade options exist:

    • persist : Cascades persist operations to the associated entities.
    • remove : Cascades remove operations to the associated entities.
    • merge : Cascades merge operations to the associated entities.
    • detach : Cascades detach operations to the associated entities.
    • all : Cascades persist, remove, merge and detach operations to associated entities.

    following docs example:

    <?php
    class User
    {
        //...
        /**
         * Bidirectional - One-To-Many (INVERSE SIDE)
         *
         * @OneToMany(targetEntity="Comment", mappedBy="author", cascade={"persist", "remove"})
         */
        private $commentsAuthored;
        //...
    }
    

    When you add comments to the author, they get persisted as you save them – when you delete the author, comments say farewell too.

    I had same issues when setting up a REST service lately, and cascade annotation got me rid of all the workarounds you mentioned before [which I used at the very beginning] – hope this was helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why, but this code worked for me a month ago... maybe
Don't know if this is possible, but I have some code like this: val
don't know if the title describes anything about what I'm trying to say but
don't know what happened in my jcrop selection. I think I pressed some keys
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know how to explain it better but i'm trying to get a response

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.