Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Doctrine2 ManyToMany Composite key Column name referenced does not exist

I have a ManyToMany relation with a composite key on the reverse side. When I use the console command doctrine:schema:update I have the following error:

[Doctrine\ORM\ORMException]
Column name `keyword` referenced for relation from Map\MapBundle\Entity\
Student towards Map\MapBundle\Entity\SkillType does not exist.

I have an entity student (unique key) with a ManyToMany relation with an entity skill (composite key) which has a ManyToOne relation with skillType (unique key).

Here is the different class mapping I have:

Class Student

<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Student
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Map\MapBundle\Entity\StudentRepository")
 */
class Student {
    /**
     *
     * @var integer @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Map\MapBundle\Entity\SkillType")
     * @ORM\JoinTable(name="students_skills",
     *      joinColumns={
     *      @ORM\JoinColumn(name="keyword", referencedColumnName="keyword"), 
     *      @ORM\JoinColumn(name="attribut", referencedColumnName="attribut")
     * })
     */
    private $skills;
}

Class skill

<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * Skill
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Map\MapBundle\Entity\SkillRepository")
 */
class Skill {
    /**
     * @ORM\ManyToOne(targetEntity="Map\MapBundle\Entity\skillType")
     * @ORM\JoinColumn(name="keyword", referencedColumnName="keyword")
     * @ORM\Id
     */
private $keyword;
}

Classe skillType

<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * SkillType
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Map\MapBundle\Entity\SkillTypeRepository")
 */
class SkillType {
    /**
     * @var string
     *
     * @ORM\Column(name="keyword", type="string", length=255)
     * @ORM\Id
     */
    private $keyword;
}

I tried to exchange the keyword and attribut @joinColumn lines, but I have the same error message with attribut instead of keyword.

I can't see what's wrong with my mapping. The table skill exists and has columns named keyword and attribut.

I hope that somebody will see where I made a mistake (probably a typo error like a missing character or a case mistake).

like image 604
FoW Avatar asked Feb 14 '23 08:02

FoW


2 Answers

Thank you for your answer. It helped me a lot and i succeded doing the schema update.

Here is the code I finaly used

/**
 * @ORM\ManyToMany(targetEntity="Carte\CarteBundle\Entity\Skill")
 * @ORM\JoinTable(name="students_skills",
 *      joinColumns={@ORM\JoinColumn(name="student_id", referencedColumnName="id")},
 *      inverseJoinColumns={
 *      @ORM\JoinColumn(name="keyword", referencedColumnName="keyword"), 
 *      @ORM\JoinColumn(name="attribut", referencedColumnName="attribut")
 * })
 */
private $skills;
like image 119
FoW Avatar answered Feb 16 '23 21:02

FoW


You write that you want Student to have the many-to-many relation with Skill, but you connected it with SkillType instead. And you're missing the inverseJoinColumns property and you didn't referenced Student properly.

Try the following annotation (untested and after looking at the documentation):

/**
 * @ORM\ManyToMany(targetEntity="Map\MapBundle\Entity\Skill")
 * @ORM\JoinTable(name="students_skills",
 *      joinColumns={@ORM\JoinColumn(name="student_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="skill_keyword", referencedColumnName="keyword")}
 * )
 */
private $skills;
like image 24
nietonfir Avatar answered Feb 16 '23 22:02

nietonfir