要求是返回所有未由登录用户购买的作业。当我有很多到很多的关系时(例如,已删除的工作比我可以成为成员的人多),这一切都很酷,不需要其他信息。但是,当有单独的购买表与其他2个表具有一对多关系时,就会发生问题。
用户:
class User
{
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Job", inversedBy="removedUsers")
* @ORM\JoinTable(name="users_removed_jobs",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="job_id", referencedColumnName="id")}
* )
*/
private $removedJobs;
/**
* @OneToMany(targetEntity="App\Entity\Purchase", mappedBy="user")
*/
private $purchases;
}
工作:
class Job
{
/**
* @OneToMany(targetEntity="App\Entity\Purchase", mappedBy="job")
*/
private $purchases;
}
购买:
class Purchase
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Job", inversedBy="purchases")
* @ORM\JoinColumn(name="job_id", referencedColumnName="id")
*/
private $job;
}
JobRepository:
public function getTradesmanJobs(Pagination $pagination)
{
$qb = $this->createQueryBuilder("j")
->from('App\Entity\User', 't')->where('t = :user')->setParameter('user', $this->user)
->where('j not member of t.removedJobs')
/*->where('t.purchases not member of j.purchases')*/;
return $qb->getQuery()->getResult();
}