为twig(或控制器)中的每条记录获取相关的实体编号
Get related entity number for each record in twig (or controller)
我有一个人 class 和一个小组 class。我通过 'group' 字段知道每个人属于哪个组,但我不知道组中有哪些人。为此,我必须通过查询人员 table.
来找到群组中的所有人
这里只是我的 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;
.....
这是我小组的一部分 class。
class Group
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
我想做的是计算每个组中有多少人并将其传递到一个树枝模板中,如下所示:
{% 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 %}
问题是我似乎无法找到如何计算和传递每个组中的人数。通过调用原则并获取存储库并将组对象传递给视图,所有其他数据都很容易,但是如何为我的记录传递额外的数据呢?
group.peopleNo
不是我组 class 的 属性。我得到它的唯一方法是通过 People class 并计算每个组中有多少人。但是如何通过控制器将它传递给视图呢?
使用下面的答案我得到了这个结构:
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"
}
}
我不确定如何在 twig 中遍历它以显示每个组的编号,因为它是按从 0 开始的顺序索引的,而不是按我的组 ID 索引的。我觉得如果一个群没有人就好了,如果找不到记录我就说它有0个人。信息是正确的,我有两组,一组1人,一组3人,但他们的id是1和2,不是0和1。
这是我的树枝的样子,foreach 中的 foreach 似乎是不好的做法:
{% 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 %}
在您的实体之间创建一个 One-To-Many, Bidirectional 关联,然后在 twig 中您可以访问每个组的人员
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();
}
}
并且在 twig 中,您可以使用 length 函数来获取每个组的人数
{% 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 %}
如果您不想更新您的实体,您可以编写一个连接查询
$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();
在 twig 中,您可以使用 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?
要实现你的正确加入和坚持教义协会,你必须实施 One-To-Many, Bidirectional
我有一个人 class 和一个小组 class。我通过 'group' 字段知道每个人属于哪个组,但我不知道组中有哪些人。为此,我必须通过查询人员 table.
来找到群组中的所有人这里只是我的 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;
.....
这是我小组的一部分 class。
class Group
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
我想做的是计算每个组中有多少人并将其传递到一个树枝模板中,如下所示:
{% 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 %}
问题是我似乎无法找到如何计算和传递每个组中的人数。通过调用原则并获取存储库并将组对象传递给视图,所有其他数据都很容易,但是如何为我的记录传递额外的数据呢?
group.peopleNo
不是我组 class 的 属性。我得到它的唯一方法是通过 People class 并计算每个组中有多少人。但是如何通过控制器将它传递给视图呢?
使用下面的答案我得到了这个结构:
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"
}
}
我不确定如何在 twig 中遍历它以显示每个组的编号,因为它是按从 0 开始的顺序索引的,而不是按我的组 ID 索引的。我觉得如果一个群没有人就好了,如果找不到记录我就说它有0个人。信息是正确的,我有两组,一组1人,一组3人,但他们的id是1和2,不是0和1。
这是我的树枝的样子,foreach 中的 foreach 似乎是不好的做法:
{% 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 %}
在您的实体之间创建一个 One-To-Many, Bidirectional 关联,然后在 twig 中您可以访问每个组的人员
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();
}
}
并且在 twig 中,您可以使用 length 函数来获取每个组的人数
{% 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 %}
如果您不想更新您的实体,您可以编写一个连接查询
$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();
在 twig 中,您可以使用 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?
要实现你的正确加入和坚持教义协会,你必须实施 One-To-Many, Bidirectional