2015-08-15 63 views
1

在我的模板中,我想调用一个函数来显示公司中员工的总数。员工与公司相关的部门,部门在一对多关系。在哪里创建Symfony 2模板中使用的方法

{% 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

的另一个问题是,如何正确使用Doctrine或DQL查询多个加入company.class实现这一点? 我试过这个功能,但我不知道这是否是正确的方法

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 ?> 

任何想法?

+0

一个帖子中有两个问题。很少有好消息的预兆。对于你的第一个问题,看看树枝扩展:http://symfony.com/doc/current/cookbook/templating/twig_extension.html – Cerad

+0

对于你的第二个问题,你的连接看起来不错,但看看这里:http:/ /stackoverflow.com/questions/9214471/count-rows-in-doctrine-querybuilder/9215880#9215880因为所有你需要的是一个计数。 – Cerad

回答

3

只需创建一个树枝扩展,并使用它与一个参数;是这样的:

扩展类:

<?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 %} 
+0

我写这个假设公司与员工的OneToMany关系。如果不是这样,请理解并将其应用于您的代码。 –

+0

你救了我的一天。我已经在寻找这个解决方案已经差不多2周了。 –

+0

太好了,我很高兴能帮上忙。 –