1

I am creating a Task Management Application Demo project just to get the hold of symfony2. So far I have finished CRUD operations. This is my defaultcontroller. I am calling the getNumberOfTasks() here.

namespace TaskManagerBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use TaskManagerBundle\Entity\Projects;
use TaskManagerBundle\Form\ProjectType;



class DefaultController extends Controller
{
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('TestBundle:Projects')
            ->findAll();

    $tasks = $em->getRepository('TestBundle:Projects')
            ->getNumberOfTasks();

    return $this->render('TestBundle:Default:index.html.twig', [
                'projects' => $entities,
                'tasks' => $tasks,
            ]
    );
}

This is my ProjectRepository, where I have defined the getNumberOfTasks method.

<?php

      namespace TaskManagerBundle\Entity\Repository;

     use Doctrine\ORM\EntityRepository;
     use TaskManagerBundle\Entity\Projects;
     use TaskManagerBundle\Entity\Tasks;

     /**
      * ProjectsRepository
       *
      * This class was generated by the Doctrine ORM. Add your own               custom
       * repository methods below.
       */
     class ProjectsRepository extends EntityRepository
     {
      public $id;
      public function getNumberOfTasks()
     {
       //$projects = new Projects();

      $query = $this->getEntityManager()
        ->createQuery(
                    'SELECT p FROM TestBundle:Tasks p WHERE p.projects = :project_id'
                )
        ->setParameter('project_id', $this->id )
        ->getResult();
    return count($query);
}

}

This is my index.html.twig

   {% for project in projects %}
      <tr>
         <td>
        {% if project.completed == 0 %}
            {{ tasks }}
        {% else %}
            Completed
        {% endif %}
      </td>
      </tr>
    {% endfor %}

I am trying to get the number of tasks for each project. How do I do that? In yii2 I could just do $this->id but here I am not able to do that.

1 Answer 1

2

You don't need the "getNumberOfTasks" method at all.

What you should be doing is specifying the associations in your Doctrine Entities.

You can read about it here:

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

Based on your naming, I would suspect a OneToMany relationship from Project to Task, and a ManyToOne relationship from Task to Project.

When mapped correctly, in Twig you can then use:

{{ project.tasks|length }} 

to get the number of tasks in a project. All you need to do is to pass the Project Entity to Twig and Symfony will handle the rest, including loading in all relationships.

This is the best way of doing what you are trying to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.