我如何使用学说来建立独特的关系

问题描述 投票:0回答:1

I have three table:

table 'user'
 - id
 - name

table 'task'
 - id
 - name

table 'report'
 - id
 - comment

我需要创建关系between table task and report and user,该报表具有unique field user_id and task_id (together!!!),但该report可以容纳many user and many task

如何通过学说做到这一点?请帮助

php mysql symfony orm doctrine
1个回答
0
投票

必须是这样:

  • 用户->报告一对多
  • 任务->报告一对多
  • 报告->用户ManyToOne
  • 报告->任务OneToMany
  • 具有ID,用户,任务字段的报表中的UniqueConstraint

我没有测试代码...

/**
 * @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
}
© www.soinside.com 2019 - 2024. All rights reserved.