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.
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:
[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?