我有两个实体,Projects
和Tasks
及其各自的存储库。
我正在尝试创建一个计算totalNumberOfTasks()
,totalNumberOfCompletedTasks()
和getPercentComplete()
的函数。
totalNumberOfTasks()
基本上会从任务表中获取与特定project_id
相关的所有数据。totalNumberOfCompletedTasks()
将查询与特定project_id相关的所有数据,但仅查询任务表中标记为COMPLETED
的数据。getPercentComplete()
将根据totalNumberOfTasks()
和totalNumberOfCompletedTasks()
函数计算百分比,并在视图文件中打印。我尝试在Twig文件中执行{{ project.tasks|length }} %
,该文件仅获取与该特定id
相关的任务总数。如何获取任务和已完成任务的总数,找到百分比,然后在视图文件中显示每个项目的显示位置?
对不起我的英语不好。我只是无法使问题更容易理解。
Project
实体:
<?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
实体:
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;
}
}
控制器:
<?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]);
}
}
答案 0 :(得分:3)
您可以使用Doctrine Criteria归档此问题,而不是使用自定义存储库方法。作为示例,您可以将以下方法添加到Projects Entity类:
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;
}
...
}
然后您可以在Twig模板中使用如下:
{{ project.percentComplete|number_format(2, '.', ',') }}
希望得到这个帮助。