如何在Symfony 4中使用Doctrine使两列的组合独特?

ig9co6j1  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(129)

我有一个名为'student_assignment'的表,其中有多个列,下面显示其中的两个列:
这两列也是外键。

StudentId   assignmentId
    10           7           -> allowed
    10           8           -> allowed
    11           7           -> allowed
    11           7           -> not allowed, the combination of 11 7 already exists in table

我已经在实体文件中尝试过此操作,但它不起作用。

/**
 * Webkul\CampusConnect\Entity\StudentAssignment
 *
 * @Table(name="student_assignment", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="assignment_unique", 
 *            columns={"student", "assignment"})
 *    }
 * )
 * @Entity
 */

请了解如何在symfony 4中使用ORM实现这一点。
我有一个链接,做同样的,但在Mysql.我想Symfony ORM. enter link description here的解决方案

错误:

[语意错误]类别Webkul\CampusConnect\Entity\StudentAssignment中的注解“@Table”从未汇入。您是否忘记为此注解加入“use”陈述式?

实体:

namespace Webkul\CampusConnect\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;

/**
 * Webkul\CampusConnect\Entity\StudentAssignment
 *
 * @ORM\Table(name="student_assignment", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="assignment_unique", 
 *            columns={"student", "assignment"})
 *    }
 * )
 * @Entity
 */
class StudentAssignment
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Student", inversedBy="studentAssignments")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $student;
t3psigkw

t3psigkw1#

你已经编辑了,但是你没有使用ORM作为导入的替身,这是第1个(见评论)。
然后您错过了将ORM添加到内部配置,例如,@ORM\UniqueConstraint而不是@UniqueConstraint。此外,UniqueConstraint的配置需要使用名称,而不是属性
您还没有提供连接表的OtM-MtO关系的两端,但我假设它存在。您应该:

namespace Webkul\CampusConnect\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(
 *    name="student_assignment", 
 *    uniqueConstraints={
 *        @ORM\UniqueConstraint(name="assignment_unique", columns={"student_id", "assignment_id"})
 *    }
 * )
 * @Entity
 */
class StudentAssignment
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(name="id", type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Student", inversedBy="studentAssignments")
     * @ORM\JoinColumn(name="student_id", onDelete="CASCADE")
     */
    private $student;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Assignment", inversedBy="studentAssignments")
     * @ORM\JoinColumn(name="assignment_id", onDelete="CASCADE")
     */
    private $assignment;

    // ...
}
wqsoz72f

wqsoz72f2#

Symfony ≥5.2(和Doctrine ORM ≥2.9)解决方案具有以下属性:

<?php
// ...

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; // optional, see below

//  ...

#[ORM\UniqueConstraint(name: 'foo_bar', columns: ['foo', 'bar', 'user_id'])] // notice the "_id" suffix in the relation column
#[UniqueEntity(['foo', 'bar', 'user'])] // optional Symfony-level constraint: https://symfony.com/doc/current/reference/constraints/UniqueEntity.html
class FoobarEntity
{
    #[ORM\Column(type: 'string')]
    private string $foo;

    #[ORM\Column(type: 'string')]
    private string $bar;

    // relation example
    #[ORM\ManyToOne(inversedBy: 'userFoobars')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $user = null;
 
    // ...
}

相关问题