Doctrine orm 循环收集并根据当前登录用户对其进行筛选';s级
我正在symfony3.4中开发一个RPG游戏,我有一个NPCEntity数组通过控制器传递给一个细枝视图。我使用下面的代码过滤了一些关于NPC的数据: 显示比当前用户级别高5级的NPC 细枝视图: 全国人大实体: 我的控制器:Doctrine orm 循环收集并根据当前登录用户对其进行筛选';s级,doctrine-orm,twig,symfony-3.4,Doctrine Orm,Twig,Symfony 3.4,我正在symfony3.4中开发一个RPG游戏,我有一个NPCEntity数组通过控制器传递给一个细枝视图。我使用下面的代码过滤了一些关于NPC的数据: 显示比当前用户级别高5级的NPC 细枝视图: 全国人大实体: 我的控制器: 其他人帮我解决了这个问题,我为我的实体创建了一个存储库,在那里我创建了一个函数,根据当前登录的用户过滤NPC级别 NPCTypeRepository: 更新了我的战列控制器中的操作: {% for npc in npcs | filter(npc => npc
其他人帮我解决了这个问题,我为我的实体创建了一个存储库,在那里我创建了一个函数,根据当前登录的用户过滤NPC级别 NPCTypeRepository: 更新了我的战列控制器中的操作:
{% for npc in npcs | filter(npc => npc.level < app.user.level+5) %}
<td>{{ npc.name }}</td>
<td>{{ npc.level }}</td>
namespace AppBundle\Entity;
/**
* @ORM\OneToMany(targetEntity="NPCType", mappedBy="level")
*/
private $npcs;
public function __construct()
{
$this->npcs = new ArrayCollection();
}
/**
* @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');
}
}
}
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();
}
}
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));
}