Symfony 4选民-一对一关系
我有一个应用程序实体,用于存储带有用户id的广告idSymfony 4选民-一对一关系,symfony,doctrine-orm,Symfony,Doctrine Orm,我有一个应用程序实体,用于存储带有用户id的广告id class User implements UserInterface, \Serializable { /** * @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="author", orphanRemoval=true) */ private $applications; } 应用实体 class Application { /** * @ORM
class User implements UserInterface, \Serializable
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="author", orphanRemoval=true)
*/
private $applications;
}
应用实体
class Application
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Advert", inversedBy="applications")
* @ORM\JoinColumn(nullable=false)
*/
private $advert;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="applications")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
}
我想要的是一个投票人,如果当前用户已经申请了一个特定的广告(在查看广告时),它可以拒绝一个操作
目前为止
在选举中
类AdvertApplicationsVoter扩展了Voter
{
我被弃权了,我不知道如何调试投票人
提前感谢假设您正确设置了投票者(因此使用支持和voteOnAttribute方法) 然后您只需更改您的方法:
private function dw(?User $user, Advert $advert)
{
foreach($user->getApplications() as $userApplication) {
if ($userApplication->getAdvert() === $advert) {
return false;
}
}
return true;
}
}
您的方法存在的问题是,您正在将数组集合与应用程序对象进行比较,您是否更新了投票者提供的
支持
方法?[您的工作是确定投票者是否应该对属性/主题组合进行投票。如果返回true,则voteOnAttribute()将被调用。否则,您的投票者已完成:其他投票者应处理此操作。]()我刚刚意识到我们使用的是应用程序而不是广告,您必须将主题更改为广告而不是应用程序,并将代码保留在函数中,该函数将用户的每个广告与您要比较的广告进行比较
private function dw(?User $user, Advert $advert)
{
foreach($user->getApplications() as $userApplication) {
if ($userApplication->getAdvert() === $advert) {
return false;
}
}
return true;
}
}