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
。
如何通过学说做到这一点?请帮助
必须是这样:
我没有测试代码...
/**
* @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
}