简体   繁体   English

从我的模板中的 DQL 返回数组的值

[英]Returning value of an array from DQL in my template

I'm learning Symfony 5 with a website for my work and I would like to return in the twig template the values in a {% set data = [] %} (sorry I don't know how this is called).我正在使用我的工作网站学习 Symfony 5,我想在 twig 模板中返回{% set data = [] %}中的值(对不起,我不知道它是如何调用的)。 I don't know how to do.我不知道该怎么做。 Here are the parts of my files that I am using :以下是我正在使用的文件部分:

home.html.twig : home.html.twig :

    {'value' : ':mission1' , 'label' : 'just_sent', 'class' : 'white-bg primary-H-color border-primary-H'},
    {'value' : ':mission2' , 'label' : 'waiting_customer', 'class' : 'green-bg white-color'},
    {'value' : ':mission3' , 'label' : 'in_progress', 'class' : 'secondary-bg white-color'},

MissionRepository.php : MissionRepository.php:

public function getCountMissionState(){

    $query = $this->createQueryBuilder('m')
    ->select('m.state, count(m.id)')
    ->where('m.state = :mission1')
      ->setParameter(':mission1', Mission::STATE_SENT)
    ->orwhere('m.state = :mission2')
      ->setParameter(':mission2', Mission::STATE_WAITING_CUSTOMER)
    ->orwhere('m.state = :mission3')
      ->setParameter(':mission3', Mission::STATE_PROGRESS)
    ->GroupBy('m.state');




    return $this->getCachedQuery($query->getQuery());

  }

DashboardController.php :仪表板控制器.php:

if($user->hasRole('ROLE_FREELANCE') || $user->hasRole('ROLE_ADMIN')){
      $data['candidat_count'] = $ur->getCandidatCount();
      $missionCountPerMonth = $mr->countMission(new \DateTime('2018-01-01'));

      $data['just_sent'] = $mr->getCountMissionState();
      $data['waiting_customers'] = $mr->getCountMissionState();
      $data['in_progress'] = $mr->getCountMissionState();
      }

      return $this->renderTemplate('dashboard/home.html.twig', $data);

Mission.php :任务.php:

public static function getStates(){
    return [
      self::STATE_SENT => 'just_sent',
      self::STATE_WAITING_CUSTOMER => 'waiting_customer',
      self::STATE_PROGRESS => 'in_progress',

I write the values like they appear in the repository and I know it is not the right way to do that but I'm still learning so please help me.我写的值就像它们出现在存储库中一样,我知道这不是正确的方法,但我仍在学习,所以请帮助我。 If you need more information please tell me.如果您需要更多信息,请告诉我。 ^^ ^^

I think your repository query is bad formuled, because It will return the same missions in all cases.我认为您的存储库查询表述不当,因为它在所有情况下都会返回相同的任务。

For get the query results using querybuilder you have to add: Querybuilder docs要使用 querybuilder 获取查询结果,您必须添加: Querybuilder docs

$query->getQuery()->execute();

And them in twig for EX:他们在树枝上为 EX:

{% for item in data['just_send'] %}
    <li>{{ item.name}}</li>
{% endfor %}

I hope have been undertand your question.我希望已经了解你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM