简体   繁体   English

我如何使用教义来创建独特的关系?

[英]How can I use doctrine to create unique relation?

I have 3 tables:我有3张桌子:

table 'user'
 - id
 - name

table 'task'
 - id
 - name

table 'report'
 - id
 - comment

I need to create a relationship between table "task", "report "and "user", that report has a unique field "user_id"and "task_id" together, but that "report " would contain many users and tasks.我需要在表“任务”、“报告”和“用户”之间创建一个关系,该报告有一个唯一的字段“user_id”和“task_id”,但该“报告”将包含许多用户和任务。

How can make it via Doctrine?如何通过 Doctrine 实现它?

It must be like this :它必须是这样的:

  • User->Reports OneToMany用户->报告一对多
  • Task->Reports OneToMany任务->报告一对多
  • Report->User ManyToOne报告->用户多对一
  • Report->Task ManyToOne报告->任务多对一
  • UniqueConstraint in Report with id, user, task fields报告中的 UniqueConstraint 带有 id、用户、任务字段

For the relations: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirectional对于关系: https : //www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirectional

For the UniqueConstraint: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#uniqueconstraint对于 UniqueConstraint: https ://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#uniqueconstraint

I didn't test the code...我没有测试代码...

/**
 * @Doctrine\ORM\Mapping\Entity()
 * @Doctrine\ORM\Mapping\Table(name="user")
 */
class User
{
    /**
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\Column(type="integer")
     * @Doctrine\ORM\Mapping\GeneratedValue
     */
    protected $id;

    /**
     * @Doctrine\ORM\Mapping\Column(type="text")
     */
    protected $name;

    /**
     * @Doctrine\ORM\Mapping\OneToMany(targetEntity="Report", mappedBy="user")
     */
    protected $reports;

    public function __construct() {
        $this->reports = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // addReport, removeReport, Getters and setters
}

/**
 * @Doctrine\ORM\Mapping\Entity()
 * @Doctrine\ORM\Mapping\Table(name="task")
 */
class Task
{
    /**
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\Column(type="integer")
     * @Doctrine\ORM\Mapping\GeneratedValue
     */
    protected $id;

    /**
     * @Doctrine\ORM\Mapping\Column(type="text")
     */
    protected $name;

    /**
     * @Doctrine\ORM\Mapping\OneToMany(targetEntity="Report", mappedBy="task")
     */
    protected $reports;

    public function __construct() {
        $this->reports = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // addReport, removeReport, Getters and setters
}

/**
 * @Doctrine\ORM\Mapping\Entity()
 * @Doctrine\ORM\Mapping\Table(name="report",uniqueConstraints={
 *     @Doctrine\ORM\Mapping\UniqueConstraint(name="report_user_task_idx", columns={"id", "user", "task"})
 * })
 */
class Report
{
    /**
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\Column(type="integer")
     * @Doctrine\ORM\Mapping\GeneratedValue
     */
    protected $id;

    /**
     * @Doctrine\ORM\Mapping\Column(type="text")
     */
    protected $comment;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="reports")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Task", inversedBy="reports")
     */
    protected $task;

    // Getters and setters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM