我在我的 SF 3.3 项目中使用 EasyAdmin,但我需要实现与 EasyAdmin 的构建方式不同的东西。看看下面的图片:
正如您可能注意到的,一个用户可以在多个GroupingRole
. 拥有这些信息的挑战是:
- 检查用户是否已分配给任何其他人
GroupingRole
- 如果条件满足条件,则显示警告消息“用户 A 已分配给 GroupingRole A”并阻止创建记录。(此消息可能在弹出窗口、javascript 警报或来自 Bootstrap 的警报中 - 因为 EA 已经使用它)
- 当管理员再次单击“保存更改”时,应创建记录。
我想用这种方法实现的是提醒管理员用户已经加入任何其他组,但不要阻止他创建记录。
我已经通过覆盖该prePersist
实体的方法来实现它的某些部分(见下文):
class AdminController extends BaseAdminController
{
/**
* Check if the users has been assigned to any group
*/
protected function prePersistGroupingRoleEntity($entity)
{
$usersToGroupRoleEntities = $this->em->getRepository('CommonBundle:UsersToGroupRole')->findAll();
$usersToGroupRole = [];
/** @var UsersToGroupRole $groupRole */
foreach ($usersToGroupRoleEntities as $groupRole) {
$usersToGroupRole[$groupRole->getGroupingRoleId()][] = $groupRole->getUsersId();
}
$usersInGroup = [];
/** @var Users $userEntity */
foreach ($entity->getUsersInGroup() as $userEntity) {
foreach ($usersToGroupRole as $group => $users) {
if (\in_array($userEntity->getId(), $users, true)) {
$usersInGroup[$group][] = $userEntity->getId();
}
}
}
$groupingRoleEnt = $this->em->getRepository('CommonBundle:GroupingRole');
$usersEnt = $this->em->getRepository('CommonBundle:Users');
$message = [];
foreach ($usersInGroup as $group => $user) {
foreach($user as $usr) {
$message[] = sprintf(
'The user %s already exists in %s group!',
$usersEnt->find($usr)->getEmail(),
$groupingRoleEnt->find($group)->getName()
);
}
}
}
}
我不知道如何停止要创建的记录,而是在第一次单击按钮时显示警告,因为第二次并发出警告,我应该允许创建记录。
任何人都可以给我一些想法和/或建议吗?
更新:添加实体信息
除了上面显示的代码之外,还有涉及该过程的实体:
/**
* @ORM\Entity
* @ORM\Table(name="grouping_role")
*/
class GroupingRole
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer",unique=true,nullable=false)
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="role_name", type="string", nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="role_description", type="string", nullable=false)
*/
private $description;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Schneider\QuoteBundle\Entity\Distributor", inversedBy="groupingRole")
* @ORM\JoinTable(name="grouping_to_role",
* joinColumns={
* @ORM\JoinColumn(name="grouping_role_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="DistributorID", referencedColumnName="DistributorID", nullable=false)
* }
* )
*
* @Assert\Count(
* min = 1,
* minMessage = "You must select at least one Distributor"
* )
*/
private $distributorGroup;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="CommonBundle\Entity\Users", inversedBy="usersGroup")
* @ORM\JoinTable(name="users_to_group_role",
* joinColumns={
* @ORM\JoinColumn(name="grouping_role_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="users_id", referencedColumnName="users_id", nullable=false)
* }
* )
*
* @Assert\Count(
* min = 1,
* minMessage = "You must select at least one user"
* )
*/
private $usersInGroup;
/**
* Constructor
*/
public function __construct()
{
$this->distributorGroup = new ArrayCollection();
$this->usersInGroup = new ArrayCollection();
}
}
/**
* @ORM\Entity()
* @ORM\Table(name="users_to_group_role")
*/
class UsersToGroupRole
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(type="integer",nullable=false)
* @Assert\Type(type="integer")
* @Assert\NotNull()
*/
protected $usersId;
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(type="integer", nullable=false)
* @Assert\Type(type="integer")
* @Assert\NotNull()
*/
protected $groupingRoleId;
}