Symfony ManyToMany关系在没有实体的情况下添加约束

时间:2018-02-06 11:43:55

标签: doctrine

我有ManyToMany关系,没有实体,我想为ManyToMany表添加约束,uniq字段的例子,它是如何完成的?

我的constrait示例实体

 * @ORM\Table(name="courses",
 *    uniqueConstraints={
 *        @UniqueConstraint(name="unique",
 *            columns={"courses_id", "courses_of_study_id"})
 *    }
 * )
 */
class Courses

但这是实体的approcah /在我的情况下,我不需要创建实体,只需要关系ManyToMany。此关系在courses_courses_of_study courses_idcourses_of_study_id的数据库中创建表格,我想添加uniqueConstraints

可以实体

class Courses
{
use TraitTimestampable;

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var ArrayCollection|CoursesOfStudy[]
 *
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\CoursesOfStudy", inversedBy="courses", cascade={"persist", "remove"})
 */
private $coursesOfStudy;

class CoursesOfStudy {     使用TraitTimestampable;

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var ArrayCollection|Courses[]
 *
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Courses", mappedBy="coursesOfStudy", cascade={"persist"})
 */
private $courses;

我尝试将此uniqueConstraints添加到实体Courses,但在迁移后差异我有错误

  [Doctrine\DBAL\Schema\SchemaException]                         
  There is no column with name 'courses_id' on table 'courses'.  

当我尝试

 * @ORM\Table(name="courses")
 * @ORM\Table(name="courses_courses_of_study",
 *    uniqueConstraints={
 *        @UniqueConstraint(name="unique",
 *            columns={"courses_id", "courses_of_study_id"})
 *    }
 * )
 */
class Courses

有错误

  [Doctrine\DBAL\Schema\SchemaException]                                        
  The table with name 'project.courses_courses_of_study' already exists. 

0 个答案:

没有答案