I have two entities, Projects
and Tasks
with their respective repositories.
I am trying to create a function that will calculate the totalNumberOfTasks()
, totalNumberOfCompletedTasks()
and getPercentComplete()
.
totalNumberOfTasks()
basically will fetch all the data related with a specific project_id
from the tasks table.totalNumberOfCompletedTasks()
will query all the data relevant with specific project_id but only those marked as COMPLETED
from the tasks table.getPercentComplete()
will calculate the percent based on the totalNumberOfTasks()
and totalNumberOfCompletedTasks()
functions and print in the view file.I have tried doing {{ project.tasks|length }} %
in Twig file which fetched only the total number of tasks relating to that specific id
. How do I get total number of tasks and completed tasks, find the percentage then show it in the view file where every projects are shown? Sorry for my english. I am just not being able to make the question more understandable.
Project
entity:
<?php
namespace TaskManagerBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Projects
* @ORM\Table()
* @ORM\Entity(repositoryClass="TaskManagerBundle\Entity\Repository\ProjectsRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Projects
{
/**
*
* @ORM\OneToMany(targetEntity="Tasks", mappedBy="projects")
*/
protected $tasks;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=30)
*/
private $title;
/**
* @var boolean
*
* @ORM\Column(name="completed", type="boolean")
*/
private $completed;
/**
* @var \Date
*
* @ORM\Column(name="due_date", type="date")
*/
private $dueDate;
/**
* @var \Date
*
* @ORM\Column(name="created", type="date")
*/
private $created;
/**
* @var \Date
*
* @ORM\Column(name="updated", type="date")
*/
private $updated;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Projects
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set completed
*
* @param boolean $completed
* @return Projects
*/
public function setCompleted($completed)
{
$this->completed = $completed;
return $this;
}
/**
* Get completed
*
* @return boolean
*/
public function getCompleted()
{
return $this->completed;
}
/**
* Set dueDate
*
* @param \Date $dueDate
* @return Projects
*/
public function setDueDate($dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* Get dueDate
*
* @return \Date
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* Set created
*
* @param \Date $created
* @return Projects
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \date
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param \Date $updated
* @return Projects
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \Date
*/
public function getUpdated()
{
return $this->updated;
}
/**
* @ORM\PrePersist
*/
public function setCreatedValue()
{
$this->created = new \DateTime();
}
/**
* @ORM\PreUpdate
*/
public function setUpdatedValue()
{
$this->updated = new \DateTime();
}
public function getNumberOfTasks()
{
}
/**
* Constructor
*/
public function __construct()
{
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tasks
*
* @param \TaskManagerBundle\Entity\Tasks $tasks
* @return Projects
*/
public function addTask(\TaskManagerBundle\Entity\Tasks $tasks)
{
$this->tasks[] = $tasks;
return $this;
}
/**
* Remove tasks
*
* @param \TaskManagerBundle\Entity\Tasks $tasks
*/
public function removeTask(\TaskManagerBundle\Entity\Tasks $tasks)
{
$this->tasks->removeElement($tasks);
}
/**
* Get tasks
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTasks()
{
return $this->tasks;
}
}
Tasks
entity:
namespace TaskManagerBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tasks
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="TaskManagerBundle\Entity\Repository\TasksRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Tasks
{
/**
*
* @ORM\ManyToOne(targetEntity="Projects", inversedBy="tasks")
* @ORM\JoinColumn(name="projects_id", referencedColumnName="id")
*/
protected $projects;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=30)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="date")
*/
private $updated;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="date")
*/
private $created;
/**
* @var \DateTime
*
* @ORM\Column(name="due_date", type="date")
*/
private $dueDate;
/**
* @var boolean
*
* @ORM\Column(name="completed", type="boolean")
*/
private $completed;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Tasks
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return Tasks
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set updated
*
* @param \DateTime $updated
* @return Tasks
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set created
*
* @param \DateTime $created
* @return Tasks
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set dueDate
*
* @param \DateTime $dueDate
* @return Tasks
*/
public function setDueDate($dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* Get dueDate
*
* @return \DateTime
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* Set completed
*
* @param boolean $completed
* @return Tasks
*/
public function setCompleted($completed)
{
$this->completed = $completed;
return $this;
}
/**
* Get completed
*
* @return boolean
*/
public function getCompleted()
{
return $this->completed;
}
/**
* Set projects
*
* @param \TaskManagerBundle\Entity\Projects $projects
* @return Tasks
*/
public function setProjects(\TaskManagerBundle\Entity\Projects $projects = null)
{
$this->projects = $projects;
return $this;
}
/**
* Get projects
*
* @return \TaskManagerBundle\Entity\Projects
*/
public function getProjects()
{
return $this->projects;
}
}
Controller:
<?php
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();
return $this->render('TestBundle:Default:index.html.twig', 'projects' => $entities]);
}
}
you can archive this problem using the Doctrine Criteria instead of make a custom Repository method. As Example you can add the following method to your Projects Entity class:
use Doctrine\Common\Collections\Criteria; // IMPORT THIS!
use Doctrine\ORM\Mapping as ORM;
/**
* Projects
* @ORM\Table()
* @ORM\Entity(repositoryClass="TaskManagerBundle\Entity\Repository\ProjectsRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Projects
{
...
public function getCompleteTasks()
{
$expr = Criteria::expr();
$criteria = Criteria::create();
$criteria->where($expr->eq('completed', true));
return $this->getTasks()->matching($criteria);
}
public function getNumberOfTasks()
{
return $this->getTasks()->count();
}
public function getPercentComplete()
{
$percentage = 0;
$totalSize = $this->getNumberOfTasks();
if ($totalSize>0)
{
$completedSize = $this->getCompleteTasks()->count();
$percentage = $completedSize / $totalSize * 100;
}
return $percentage;
}
...
}
Then you can use in your Twig template as follow:
{{ project.percentComplete|number_format(2, '.', ',') }}
Hope this help.