1

I have a People class and a Group class. I know what group each man is part of through its 'group' field, but I don't know what people are in a group. To do that I have to find all people in the group by querying the People table.

Here are just the relevant parts of my classes:

class People
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id",type="integer")
     */
    protected $id;


    /**
     * @ORM\ManyToOne(targetEntity="Bundle\Entity\Groups")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $group;

.....

And here is part of my Group class.

class Group
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
...

What I'm trying to do is count how many people are in each group and pass it in a twig template like so:

  {% for group in allGroups %}
            <li>name: {{group.name}} </li>
            <li>date:{{ group.createdAt|date('d-m-Y H:i:s') }}</li>
            <li>Number of people: {{ group.peopleNo}}</li>
            <hr>
   {% endfor %}

The problem is that I can't seem to find out how to calculate and pass the number of people in each group. All the other data is easy by calling the doctrine and getting the repository and passing the group object to the view, but how to pass additional data for my records?

The group.peopleNo is not a property of my Group class. The only way for me to get it is go through the People class and count for each group how many people are in the group. But how do I pass it through the controller to the view?

Using the answer below I get this structure:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(8) "Array(4)"
    ["total_peoples"]=>
    string(1) "3"
  }
  [1]=>
  array(2) {
    [0]=>
    string(8) "Array(4)"
    ["total_peoples"]=>
    string(1) "1"
  }
}

I'm not sure how can I loop through it in twig to display each group's number because it's indexed in order starting with 0, rather than indexed by my group id's. I think it's fine if a group has no people, then I'll just say it has 0 people if it can't find a record with it. The information is correct, I have two groups, one with 1 and other with 3 people, but their id's are 1 and 2, not 0 and 1.

Here's how my twig would look like, foreach in foreach seems like bad practice:

   {% for group in groups %}
            <li> {{ group .name|e }}</li>
            <li>{{ group .createdAt|date('d-m-Y H:i:s') }}</li>
            <li>
                {% for key, ppno in peopleno %}
                    {% if ppno [key] is defined %}
                        {% if ppno [key]['id']==ticket.id %}
                            people: {{ peopleno [key]['total_peoples'] }}
                        {% endif %}
                    {% else %}
                        people:0
                    {% endif %}

                {% endfor %}
            </li>
            <hr>
        {% endfor %}
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88

1 Answers1

1

Create a One-To-Many, Bidirectional association between your entities and then in twig you can access peoples for each group

use Doctrine\Common\Collections\ArrayCollection;
class Group
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
...
    /**
     * @ORM\OneToMany(targetEntity="People", mappedBy="group")
     **/
    private $peoples;
    // ...

    public function __construct() {
        $this->peoples = new ArrayCollection();
    }


}

And in twig you can use length function to get the count of peoples for each group

{% for group in allGroups %}
            <li>name: {{group.name}} </li>
            <li>date:{{ group.createdAt|date('d-m-Y H:i:s') }}</li>
            <li>Number of people: {{ group.peoples|length }}</li>
            <hr>
{% endfor %}

If you don't want to update your entities you can write a join query

$DM = $this->getDoctrine()->getManager();
$peopleRepo = $DM->getRepository('People entity');
$qb = $peopleRepo->createQueryBuilder('p')
    ->select('g.name,g.createdAt,COUNT(p.id) AS total_peoples')
    ->innerJoin('p.group', 'g')
    ->groupBy('g.id')
    ->getQuery()
    ->getArrayResult();

In twig you can loop through your results and display count using total_peoples index

Note this will not return groups that don't have any associated users means group with 0 count will not be returned because its an inner join , what you need is right join to group entity by unfortunately its not possible to have a right join in dql Doctrine DQL RIGHT JOIN?

To achieve your right join thing and sticking to doctrine association you have to implement One-To-Many, Bidirectional

Community
  • 1
  • 1
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • Is there no other way without modifying the entities at all? – George Irimiciuc Aug 31 '15 at 17:21
  • Please do, I can't really modify the entities. – George Irimiciuc Aug 31 '15 at 17:29
  • I added the structure that I get at the end of my post. I'm not sure how can I loop through it in twig to display each group's number because it's indexed in order starting with 0, rather than indexed by my group id's. I think it's fine if a group has no people, then I'll just say it has 0 people. – George Irimiciuc Aug 31 '15 at 17:49
  • @GeorgeIrimiciuc use my currnet query i guess you copied before my last update this should give you single array with all rows – M Khalid Junaid Aug 31 '15 at 17:56
  • No, it's this one, I just removed g.name,g.createdAt and put g.id instead. The format is still the same. – George Irimiciuc Aug 31 '15 at 18:00
  • @GeorgeIrimiciuc you can loop your results and can get something like in php `foreach ($groups as $group) {$group[0]['total_peoples'] ...` in twig you can write its equivalent code – M Khalid Junaid Aug 31 '15 at 18:13
  • I know but group[0] is not the group with the id 0. Do I have to do a foreach in the foreach that finds everytime the $group[x]['id']==$group.id and then get $group[x]['total_peoples']? I have to do a foreach loop to calculate x, which is the index in my array. Sounds a bit of a bad practice to do a foreach in a foreach to me. – George Irimiciuc Aug 31 '15 at 18:18
  • I added in my post how the twig looks and what I'm talking about. There should be a better way than having to loop through the array for each group. – George Irimiciuc Aug 31 '15 at 18:36