1

In my template, I want to call a function that will display the total counts of of Employee in Company.Employee is related to Department, Department related to Company in one to many relationships.

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

In controller

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

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

Where should I put the getNumberOfEmp method?

In Symfony 1.4, I easily achieved this by putting the getNumberOfEmp in the company.class that will call the company.table.class

Another question is, how to properly use Doctrine or DQL to query multiple Join? I tried this function but I don't know if it is the proper way

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() 
    ;
 }

In Symfony 1.4 I use it this way

//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();
    }

And easily call it in php template

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

Any Ideas?

Danielle Rose Mabunga
  • 1,676
  • 4
  • 25
  • 48
  • Two questions in one post. Seldom a harbinger of good tidings. For your first question, take a look at twig extensions: http://symfony.com/doc/current/cookbook/templating/twig_extension.html – Cerad Aug 15 '15 at 11:46
  • For your second question, your joins look ok but take a look here: http://stackoverflow.com/questions/9214471/count-rows-in-doctrine-querybuilder/9215880#9215880 since all you need is a count. – Cerad Aug 15 '15 at 11:49

1 Answers1

3

Just a create a twig extension, and use with it with an argument; something like:

The extension class:

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

}

}

Define your extension in service.yml and inject the entity manager:

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

and finally you can use it in your template like :

{% for com in company %}
  {{ com.name }}
  {{ com.description }}
  {{ number_employees(com.id) }} 
{% endfor %}
Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21