dongzhang3482 2019-04-23 06:58
浏览 47
已采纳

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

I have a table named 'student_assignment' in which I have multiple columns from which I am showing 2 of them below:

Both of these columns are also foreign keys.

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

I have tried this in my entity file, but it does not work.

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

Please how to implement this using ORM in symfony 4.

I have a link which does ther same but in Mysql. I want the solution for Symfony ORM. enter link description here

Error:

[Semantical Error] The annotation "@Table" in class Webkul\CampusConnect\En tity\StudentAssignment was never imported. Did you maybe forget to add a "u se" statement for this annotation?

Entity:

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;
  • 写回答

1条回答 默认 最新

  • doujia6433 2019-04-23 10:36
    关注

    You've edited, but you weren't using ORM as an imported alias, that was number 1 (see comments).

    Then you missed adding ORM to the inner configuration, e.g. @ORM\UniqueConstraint instead of @UniqueConstraint. Also, the configuration of UniqueConstraint requires the use of the column names, not property.

    You've not provided both sides of the join table'esque OtM - MtO relation, but I'll assume it exists. You should have:

    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;
    
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?