Steinbock
Steinbock

Reputation: 868

Symfony Doctrine Many2Many relationship works only one way

I'm developing a project management system with Symfony2. I have a project which can have one or more categories (many2many relation). After generating the two entities, forms and controllers with symfony, I can create a new category and assign some projects to it (categoryproject/1/edit) but I can't do the oposit, means I can't assign categories to a project in my project edit form (every time when i choose some categories and press update the choosen fields get white again). How is that possible? Here is my code:

The project entity: Dbe\DDBundle\Entity\Project:

manyToMany:
categoryProject:
  targetEntity: Dbe\DDBundle\Entity\CategoryProject
  mappedBy: project

The categoryProject doctrine:

  manyToMany:
project:
  targetEntity: Dbe\DDBundle\Entity\Project
  inversedBy: categoryProject
  joinTable:
    name: ProjectToCategoryProject
    joinColumns:
      category_project_id:
        referencedColumnName: id
        nullable: false
    inverseJoinColumns:
      project_id:
        referencedColumnName: id
        nullable: false

Here is the query i get from the Symfony Toolbar: why is there a questionmark?

    SELECT 
  t0.id AS id1, 
  t0.name AS name2, 
  t0.description AS description3 
FROM 
  CategoryProject t0 
  INNER JOIN ProjectToCategoryProject ON t0.id = ProjectToCategoryProject.category_project_id 
WHERE 
  ProjectToCategoryProject.project_id = ?

And here the add function:

    /**
 * Add categoryProject
 *
 * @param \Dbe\DDBundle\Entity\CategoryProject $categoryProject
 * @return Project
 */
public function addCategoryProject(\Dbe\DDBundle\Entity\CategoryProject $categoryProject)
{


    $categoryProject->addCategory($this);
    $this->categoryProjects[] = $categoryProject;

   // $this->categoryProject[] = $categoryProject;

    return $this;
}

Thanks in advance guys for your help!

Upvotes: 1

Views: 404

Answers (1)

Steinbock
Steinbock

Reputation: 868

Here is the solution, I made some error in reasoning :-)

    $entity = $em->getRepository('DbeddddBundle:Project')->findOneById($id);
    $comment = $em -> getRepository('DbeDddddBundle:Comment') -> findByProject($entity);

Upvotes: 1

Related Questions