【问题标题】:Where to create method to use in templates in Symfony 2在 Symfony 2 的模板中创建使用方法的位置
【发布时间】:2015-11-08 13:09:45
【问题描述】:

在我的模板中,我想调用一个函数来显示 Company 中 Employee 的总数。Employee 与 Department 相关,Department 与 Company 以一对多的关系关联。

{% for com in company %}
    {{ com.name }}
    {{ com.description }}
    {{ com.getNumberOfEmp|length }} //this a function must display counts of employee
{% endfor %}

在控制器中

$em = $this->getDoctrine()->getManager();

    $company = $em->getRepository('Bundle:Company')->findAll();

我应该把 getNumberOfEmp 方法放在哪里?

在 Symfony 1.4 中,我通过将 getNumberOfEmp 放入将调用 company.table.class 的 company.class 中轻松实现了这一点

另一个问题是,如何正确使用Doctrine或DQL查询多个Join? 我试过这个功能,但我不知道它是否正确

companyrepository.php

public function getNumberOfEmp()
{
return $this
      ->createQueryBuilder()
      ->select('e.firstname')
      ->from('Emp e')
      ->leftJoin('e.Department d')
      ->leftJoin('d.Company c')
      ->where('i.id =:$id)  
      ->setParameter('id',$this->id)// I am confused about this since i want to display all names of the company
      ->getQuery()
      ->getResult() 
    ;
 }

在 Symfony 1.4 中我以这种方式使用它

//company.class.php

public function getNumberOfEmp()
{
    $emp = Doctrine_Core::getTable('Company')->createQuery('c')
    ->select('v.firstname')
            ->from('Employeers e')
            ->leftJoin('e.Department d')
            ->leftJoin('d.Company c')
            ->where('c.id=?',$this->id);
            return $emp->execute();
    }

并在php模板中轻松调用它

<?php foreach ($company as $com): ?>
   <?php echo $com->name ?>/display name of company
   <?php echo $com->description ?>//description
   <?php echo count($com.getNumberOfEmp) ?>//dispalys number of employees
<?php endforeach ?>

有什么想法吗?

【问题讨论】:

标签: symfony doctrine-orm twig dql


【解决方案1】:

只需创建一个 twig 扩展,并将其与参数一起使用;类似:

扩展类:

<?php

namespace WHERE\YOU_WANT\TO\CREATE_IT;

class TestExtension extends \Twig_Extension
{
 protected $em;

 public function __construct($em)
 {
    $this->em = $em;
 }

 public function getFunctions()
 {
    return array(
       //this is the name of the function you will use in twig
        new \Twig_SimpleFunction('number_employees', array($this, 'a'))
    );
}

public function getName()
{
    return 'nbr_employees';
}   

public function a($id)
{
    $qb=$this->em->createQueryBuilder();
    $qb->select('count(n.id)')
       ->from('XYZYOurBundle:Employee','n')
       ->where('n.company = :x)
       ->setParameter('x',$id);
    $count = $qb->getQuery()->getSingleScalarResult(); 

    return $count;

}

}

在 service.yml 中定义你的扩展并注入实体管理器:

    numberemployees:
        class: THE\EXTENSION\NAMESPACE\TestExtension
        tags:
            - { name: twig.extension }
        arguments:
            em: "@doctrine.orm.entity_manager"

最后你可以在你的模板中使用它:

{% for com in company %}
  {{ com.name }}
  {{ com.description }}
  {{ number_employees(com.id) }} 
{% endfor %}

【讨论】:

  • 我写这篇文章是假设公司与员工建立 OneToMany 关系。如果不是,请了解这个想法并将其适应您的代码。
  • 你拯救了我的一天。我已经寻找这个解决方案将近 2 周了
  • 太好了,很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多