统计相关实体中的记录 [英] Count records in related entity

查看:20
本文介绍了统计相关实体中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与城市有关的选民实体,城市与省有关,省与地区有关,地区与岛屿有关.每个实体的关系是一对多关系.我想要的是统计特定岛屿中的所有选民.所以在控制器中,

I have a voter entity that is related to city, city is related to province, province is related to region and region is related to island.The relation of each entity is One-To-Many relationship .What I want is to count all voters in a particular island.So In the controller ,

//voterscontroller.php
public function islandAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $islands = $em->getRepository('DuterteBundle:Island')->findAll();

    return $this->render('DuterteBundle:Voters:islands.html.twig', array(
        'islands' => $islands,
    ));
}

我在模板中试过这个

<tr>
        <th>#</th>
        <th>Island</th>
        <th>Votes</th>
    </tr>
    {% for island in islands %}
    <tr {% if loop.index is odd %}class="color"{% endif %}>
        <td>{{ island.id }}</td>
        <td>{{ island.name }}</td>
        <td>{% if island.region %}{{ island.region.province.city.voters|length }}{% endif %}</td>//I want to count of voters
    </tr>
    {% endfor %}

错误是

第 17 行的 DuterteBundle:Voters:islands.html.twig 中不存在对象DoctrineORMPersistentCollection"的方法province"

Method "province" for object "DoctrineORMPersistentCollection" does not exist in DuterteBundle:Voters:islands.html.twig at line 17

我试图删除一些这样的实体

I tried to remove some entity like this

<td>{% if island.region %}{{ island.region|length }}{% endif %}</td>

它有效.它显示了地区的总数.但我需要计算选民人数.任何想法如何实现这一点?

And it works.It shows the total count of region.But I need to count the voters..Any Idea how to achieve this?

更新

//province.php

<?php

namespace ProjectBundleDuterteBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * Province
 */
class Province
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var string
 */
private $createdBy;

/**
 * @var DateTime
 */
private $dateCreated;

/**
 * @var string
 */
private $updatedBy;

/**
 * @var DateTime
 */
private $dateUpdated;

/**
 * @var integer
 */
private $regionId;

public function __toString()
{
    return $this->name;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Province
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set createdBy
 *
 * @param string $createdBy
 * @return Province
 */
public function setCreatedBy($createdBy)
{
    $this->createdBy = $createdBy;

    return $this;
}

/**
 * Get createdBy
 *
 * @return string 
 */
public function getCreatedBy()
{
    return $this->createdBy;
}

/**
 * Set dateCreated
 *
 * @param DateTime $dateCreated
 * @return Province
 */
public function setDateCreated($dateCreated)
{
    $this->dateCreated = $dateCreated;

    return $this;
}

/**
 * Get dateCreated
 *
 * @return DateTime 
 */
public function getDateCreated()
{
    return $this->dateCreated;
}

/**
 * Set updatedBy
 *
 * @param string $updatedBy
 * @return Province
 */
public function setUpdatedBy($updatedBy)
{
    $this->updatedBy = $updatedBy;

    return $this;
}

/**
 * Get updatedBy
 *
 * @return string 
 */
public function getUpdatedBy()
{
    return $this->updatedBy;
}

/**
 * Set dateUpdated
 *
 * @param DateTime $dateUpdated
 * @return Province
 */
public function setDateUpdated($dateUpdated)
{
    $this->dateUpdated = $dateUpdated;

    return $this;
}

/**
 * Get dateUpdated
 *
 * @return DateTime 
 */
public function getDateUpdated()
{
    return $this->dateUpdated;
}

/**
 * Set regionId
 *
 * @param integer $regionId
 * @return Province
 */
public function setRegionId($regionId)
{
    $this->regionId = $regionId;

    return $this;
}

/**
 * Get regionId
 *
 * @return integer 
 */
public function getRegionId()
{
    return $this->regionId;
}
/**
 * @var DoctrineCommonCollectionsCollection
 */
private $city;

/**
 * @var ProjectBundleDuterteBundleEntityRegion
 */
private $region;

/**
 * Constructor
 */
public function __construct()
{
    $this->city = new DoctrineCommonCollectionsArrayCollection();
}

/**
 * Add city
 *
 * @param ProjectBundleDuterteBundleEntityCity $city
 * @return Province
 */
public function addCity(ProjectBundleDuterteBundleEntityCity $city)
{
    $this->city[] = $city;

    return $this;
}

/**
 * Remove city
 *
 * @param ProjectBundleDuterteBundleEntityCity $city
 */
public function removeCity(ProjectBundleDuterteBundleEntityCity $city)
{
    $this->city->removeElement($city);
}

/**
 * Get city
 *
 * @return DoctrineCommonCollectionsCollection 
 */
public function getCity()
{
    return $this->city;
}

/**
 * Set region
 *
 * @param ProjectBundleDuterteBundleEntityRegion $region
 * @return Province
 */
public function setRegion(ProjectBundleDuterteBundleEntityRegion $region = null)
{
    $this->region = $region;

    return $this;
}

/**
 * Get region
 *
 * @return ProjectBundleDuterteBundleEntityRegion 
 */
public function getRegion()
{
    return $this->region;
}
}

region.php

<?php

namespace ProjectBundleDuterteBundleEntity;

use DoctrineORMMapping as ORM;

/**
* Region
*/
class Region
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var integer
 */
private $islandId;

/**
 * @var string
 */
private $createdBy;

/**
 * @var DateTime
 */
private $dateCreated;

/**
 * @var string
 */
private $updatedBy;

/**
 * @var DateTime
 */
private $dateUpdated;

public function __toString()
{
    return $this->name;
}
/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Region
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set islandId
 *
 * @param integer $islandId
 * @return Region
 */
public function setIslandId($islandId)
{
    $this->islandId = $islandId;

    return $this;
}

/**
 * Get islandId
 *
 * @return integer 
 */
public function getIslandId()
{
    return $this->islandId;
}

/**
 * Set createdBy
 *
 * @param string $createdBy
 * @return Region
 */
public function setCreatedBy($createdBy)
{
    $this->createdBy = $createdBy;

    return $this;
}

/**
 * Get createdBy
 *
 * @return string 
 */
public function getCreatedBy()
{
    return $this->createdBy;
}

/**
 * Set dateCreated
 *
 * @param DateTime $dateCreated
 * @return Region
 */
public function setDateCreated($dateCreated)
{
    $this->dateCreated = $dateCreated;

    return $this;
}

/**
 * Get dateCreated
 *
 * @return DateTime 
 */
public function getDateCreated()
{
    return $this->dateCreated;
}

/**
 * Set updatedBy
 *
 * @param string $updatedBy
 * @return Region
 */
public function setUpdatedBy($updatedBy)
{
    $this->updatedBy = $updatedBy;

    return $this;
}

/**
 * Get updatedBy
 *
 * @return string 
 */
public function getUpdatedBy()
{
    return $this->updatedBy;
}

/**
 * Set dateUpdated
 *
 * @param DateTime $dateUpdated
 * @return Region
 */
public function setDateUpdated($dateUpdated)
{
    $this->dateUpdated = $dateUpdated;

    return $this;
}

/**
 * Get dateUpdated
 *
 * @return DateTime 
 */
public function getDateUpdated()
{
    return $this->dateUpdated;
}
/**
 * @var DoctrineCommonCollectionsCollection
 */
private $province;

/**
 * @var ProjectBundleDuterteBundleEntityIsland
 */
private $island;

/**
 * Constructor
 */
public function __construct()
{
    $this->province = new DoctrineCommonCollectionsArrayCollection();
}

/**
 * Add province
 *
 * @param ProjectBundleDuterteBundleEntityProvince $province
 * @return Region
 */
public function addProvince(ProjectBundleDuterteBundleEntityProvince $province)
{
    $this->province[] = $province;

    return $this;
}

/**
 * Get province
 *
 * @return DoctrineCommonCollectionsCollection 
 */
public function getProvince()
{
    return $this->province;
}

/**
 * Set island
 *
 * @param ProjectBundleDuterteBundleEntityIsland $island
 * @return Region
 */
public function setIsland(ProjectBundleDuterteBundleEntityIsland $island = null)
{
    $this->island = $island;

    return $this;
}

/**
 * Get island
 *
 * @return ProjectBundleDuterteBundleEntityIsland 
 */
public function getIsland()
{
    return $this->island;
}

/**
 * Remove province
 *
 * @param ProjectBundleDuterteBundleEntityProvince $province
 */
public function removeProvince(ProjectBundleDuterteBundleEntityProvince $province)
{
    $this->province->removeElement($province);
}
}

更新

为什么错误说一个方法不存在,而实际上它存在于实体中.另外我在调试工具栏中没有错误.都说映射是有效的

Why the errors says a method does not exist when in fact it exists in entity.Also I have no error in debug toolbar.It all says that mapping is valid

更新

我像这样更改了我的 Twig 模板,错误消失了,但在表格中显示了意外的格式

I change my Twig template like this, the errors are gone but display unexpected formats in the table

{% for island in islands %}
    <tr {% if loop.index is odd %}class="color"{% endif %}>
        <td>{{ island.id }}</td>
        <td>{{ island.name }}</td>
        <td>
            {% for region in island.region %}
                {% for province in region.province %}
                    {% for city in province.city %}
                        {{ city.voters|length }}
                    {% endfor %}
                {% endfor %}
            {% endfor %}
        </td>
    </tr>
    {% endfor %}

Id 4 'OFW' 呈现正确的数字,但其余的呈现奇怪的整数.我知道原因,因为 Island('OFW') 记录中只有一个地区、一个省、一个城市和许多选民,而rests(Mindanao, Luzon, Visayas) 在每条记录中都有许多地区、省、市.如何修复其他 3 个岛屿(Mindanao, Visayas, Luzon) 以呈现像OFW"岛这样的数字?

Id number 4 'OFW' renders correct number, but the rest renders weird integers.I know the reason since Island('OFW') has only one region, one province, one city and many voters in the record, while the rests(Mindanao, Luzon, Visayas) has a many regions, province, city in each record.How to fix the other 3 islands(Mindanao, Visayas, Luzon) to render numbers like the "OFW" island?

//更新

由于之前的解决方案混乱",我放弃了循环.我尝试使用 DQL

I give up looping in the previous solution since its 'messy'.I tried instead DQL

repository.php

public function findAllAll()
{
$query = $this->getEntityManager()->createQuery(
  'SELECT i,r,p FROM DuterteBundle:Island i
  JOIN i.region r
  JOIN r.province p
  WHERE i.name = :name'
)->setParameter('name', 'Mindanao');
  try {
    return $query->getSingleResult();
  } catch (DoctrineORMNoResultException $e) {
    return null;
  }
}

但当我尝试转储"岛时显示 null

but display null when I tried to 'dump' island

{{ dump(islands) }}//return null
    {% for island in islands %}
    <tr>
        <td>{{ island.id }}</td>
        <td>{{ island.name }}</td>
        <td>{{ island.region.province.city.voters|length }}</td>
    </tr>
    {% endfor %}

推荐答案

第 17 行的 DuterteBundle:Voters:islands.html.twig 中不存在对象DoctrineORMPersistentCollection"的方法province"

Method "province" for object "DoctrineORMPersistentCollection" does not exist in DuterteBundle:Voters:islands.html.twig at line 17

我认为第 17 行就是那个

I think the line 17 is that one

{{ island.region.province.city.voters|length }}

错误清楚地告诉你,实体 Island 内的属性 region 不是单个实体,而是实体的集合;你应该检查你的 Island 类定义.

The error is clearly telling you that the property region inside the entity Island is not a single entity but a collection of entity; you should check your Island class definition.

这篇关于统计相关实体中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆