循环遍历集合并根据当前登录用户的级别对其进行过滤
Loop through collection and filter it based by current logged-in user's level
我正在 Symfony 3.4 中开发一款 RPG 游戏,我有一个 NPCEntity 数组通过控制器传递给一个树枝视图。我使用下面的代码来过滤一些关于 NPC 的数据:
显示比当前用户等级高 5 级的 NPC。
树枝视图:
{% for npc in npcs | filter(npc => npc.level < app.user.level+5) %}
<td>{{ npc.name }}</td>
<td>{{ npc.level }}</td>
但是,我必须为关卡系统创建单独的实体,并且我将 NPC 实体的 $level 属性 ManyToOne 映射到关卡实体的 $npcs 属性 其中 returns数组集合,现在我一直在过滤视图中的数据。
等级实体:
namespace AppBundle\Entity;
/**
* @ORM\OneToMany(targetEntity="NPCType", mappedBy="level")
*/
private $npcs;
public function __construct()
{
$this->npcs = new ArrayCollection();
}
NPC实体:
/**
* @ORM\ManyToOne(targetEntity="Level")
*/
private $level;
我的控制器:
class BattlesController extends Controller
{
/**
* @Route("/battles", name="battles")
*/
public function indexAction(Request $request)
{
$auth_checker = $this->get('security.authorization_checker');
if ($auth_checker->isGranted('IS_AUTHENTICATED_FULLY')) {
$npcs = $this->getDoctrine()
->getRepository('AppBundle:NPCType')
->findAll();
return $this->render('default/battles.html.twig', array('npcs' => $npcs));
} else {
return $this->redirectToRoute('fos_user_security_login');
}
}
}
其他人帮我解决了这个问题,我为我的实体创建了一个存储库,并在那里创建了一个功能来根据当前登录的用户过滤 NPC 级别。
NPCTypeRepository:
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class NPCTypeRepository extends EntityRepository
{
public function findAllBetween($currentLevel, $limitLevel) {
$qb = $this->createQueryBuilder('n');
return $qb->andWhere($qb->expr()->between('n.level', $currentLevel, $limitLevel))
->getQuery()
->getResult();
}
}
更新了我的 BattlesController 中的操作:
public function indexAction(Request $request)
{
$user = $this->getUser();
$npcs = $this->getDoctrine()
->getRepository(NPCType::class)->findAllBetween($user->getLevel(), $user->getLevel() + 5);
return $this->render('default/battles.html.twig', array('npcs' => $npcs));
}
我正在 Symfony 3.4 中开发一款 RPG 游戏,我有一个 NPCEntity 数组通过控制器传递给一个树枝视图。我使用下面的代码来过滤一些关于 NPC 的数据: 显示比当前用户等级高 5 级的 NPC。
树枝视图:
{% for npc in npcs | filter(npc => npc.level < app.user.level+5) %}
<td>{{ npc.name }}</td>
<td>{{ npc.level }}</td>
但是,我必须为关卡系统创建单独的实体,并且我将 NPC 实体的 $level 属性 ManyToOne 映射到关卡实体的 $npcs 属性 其中 returns数组集合,现在我一直在过滤视图中的数据。
等级实体:
namespace AppBundle\Entity;
/**
* @ORM\OneToMany(targetEntity="NPCType", mappedBy="level")
*/
private $npcs;
public function __construct()
{
$this->npcs = new ArrayCollection();
}
NPC实体:
/**
* @ORM\ManyToOne(targetEntity="Level")
*/
private $level;
我的控制器:
class BattlesController extends Controller
{
/**
* @Route("/battles", name="battles")
*/
public function indexAction(Request $request)
{
$auth_checker = $this->get('security.authorization_checker');
if ($auth_checker->isGranted('IS_AUTHENTICATED_FULLY')) {
$npcs = $this->getDoctrine()
->getRepository('AppBundle:NPCType')
->findAll();
return $this->render('default/battles.html.twig', array('npcs' => $npcs));
} else {
return $this->redirectToRoute('fos_user_security_login');
}
}
}
其他人帮我解决了这个问题,我为我的实体创建了一个存储库,并在那里创建了一个功能来根据当前登录的用户过滤 NPC 级别。
NPCTypeRepository:
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class NPCTypeRepository extends EntityRepository
{
public function findAllBetween($currentLevel, $limitLevel) {
$qb = $this->createQueryBuilder('n');
return $qb->andWhere($qb->expr()->between('n.level', $currentLevel, $limitLevel))
->getQuery()
->getResult();
}
}
更新了我的 BattlesController 中的操作:
public function indexAction(Request $request)
{
$user = $this->getUser();
$npcs = $this->getDoctrine()
->getRepository(NPCType::class)->findAllBetween($user->getLevel(), $user->getLevel() + 5);
return $this->render('default/battles.html.twig', array('npcs' => $npcs));
}