D
D
Dasha2016-04-11 17:07:22
symfony
Dasha, 2016-04-11 17:07:22

Why doesn't manyToMany binding work in symfony 2?

There are two tables, company and rubric, and a company_rubric relationship table, each company can have multiple categories and each category can also have multiple companies. they are linked by id.
Here is what I wrote (I only insert what is relevant to the connections):

RG\JuristBundle\Entity\Company:
    type: entity
    table: company

    manyToMany:
        rubrics:
            targetEntity: Rubric
            inversedBy: companies
            joinTable:
               name: jurist.company_rubric


RG\JuristBundle\Entity\Rubric:
    type: entity
    table: rubric
   
    manyToMany:
        companies:
            targetEntity: Company
            mapped_by: rubrics
            joinTable:
              name: jurist.company_rubric
              joinColumns:
                rubric_id:
                  referencedColumnName: id
              inverseJoinColumns:
                company_id:
                  referencedColumnName: id

added to the corresponding Entities:
class Rubric{
private $companies;

    public function __construct()
    {
        $this->companies = new ArrayCollection();
    }
}

class Company{
    private $rubrics;
    public function __construct()
    {
        $this->rubrics = new ArrayCollection();
    }
}

When in the controller I try to get categories or companies like this:
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('RGJuristBundle:Rubric')->find($id);
Arrays that should be filled with data due to links remain empty. Maybe I wrote something wrong or I'm doing something wrong? I've never dealt with symphony before, so it's hard to figure it out.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Romanenko, 2016-04-11
@slimus

0. there is a tag for inserting code
1. clear the cache
2. ./app/console doctrine:schema:validate and show the result

A
Alexander Evgenievich, 2016-04-12
@banderos120

manyToMany:
        rubrics:
            targetEntity: Rubric
            mappedBy: companies

targetEntity: Company
inversedBy: rubrics
joinColumns:
   name: rubric_id
   referencedColumnName: id
inverseJoinColumns:
   name: company_id
   referencedColumnName: id

doctrine-orm.readthedocs.org/projects/doctrine-orm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question