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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:55:43+00:00 2026-06-09T17:55:43+00:00

I have a many to many relationship set up between listings and categories. I

  • 0

I have a many to many relationship set up between listings and categories. I am trying to set the categories for a listing using $listing->setCategories($categories). I do not get errors but the relationship isn’t saved to the db.

I will include all of the relevant code below.

Mapping Files

Listing Mapping

<many-to-many field="categories" target-entity="Default_Model_Category" mapped-by="listings" >
     <cascade>
         <cascade-persist/>
     </cascade>
 </many-to-many>

Categrory mapping

 <many-to-many field="listings" target-entity="Default_Model_Listing" inversed-by="categories"/>

I have all the relevant variables and get/set functions in the model files.

trying to save the relationship in the controller.

    $catModel = $this->em->getRepository('Default_Model_Category');
    $catArray = array();
    foreach($categories as $single) {
       $catArray[] = $catModel->findOneById($single);
    }
    $listing->setCategories($catArray);
    $this->em->flush();

thanks for your help.

  • 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-09T17:55:44+00:00Added an answer on June 9, 2026 at 5:55 pm

    I have had a similar bug. I don’t know what was causing it, but I had to save an empty array of values before saving the new relationships:

    eg:

    $listing->setCategories(array()); //clear current relationships
    $this->em->flush();
    $listing->setCategories($catArray); // save new relationships
    $this->em->flush();
    

    [edit]

    It looks like you are assigning parent categories to a child listing. In simple terms, Doctrine is saying that the parent is right and the child is wrong so the child listing cannot assign a new parent. Try reversing the code?

    $catModel = $this->em->getRepository('Default_Model_Category');
    foreach($categories as $single) {
        $cat = $catModel->findOneById($single);
        $listingArray = $cat->getListings();
        $listingArray[] = $listing;
        $cat->setListings($listingArray);
        $this->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 many-to-many relationship defined in my Symfony (using doctrine) project between Orders
Let's say I have two objects: Articles and Categories with a many-many relationship between
I have a simple many to many relationship set up between tasks and tags.
So, we have a many-to-many relationship between Customer and Role, set up as: Customer
I'm trying to define a simple one-to-many relationship between two poco's , using the
I have set up a HABTM relationship between two table creating a many to
I have many-to-one mappings working fine, but a one-to-many relationship between locations and location_times
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I have a many to many relationship: Product has many Categories and Category has

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.