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

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

问题描述

我有一个与城市有关的选民单位,城市是与省有关,省与区域有关,地区与岛屿有关。每个实体的关系是一对多的关系,我想要的是计算一个特定岛屿中的所有选民。因此,在控制器中,

  // votcontroller.php 
public function islandAction请求$ 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>岛< / th>
< th>投票< / th>
< / tr>
{%在岛屿中的岛屿%}
< tr {%如果loop.index是奇数%} class =color{%endif%}>
< td> {{island.id}}< / td>
< td> {{island.name}}< / td>
< td> {%if island.region%} {{island.region.province.city.voters | length}} {%endif%}< / td> //我要计数的选民
< / tr>
{%endfor%}

错误是


DuterteBundle中不存在对象Doctrine\ORM\PersistentCollection的方法province:选民:islands.html.twig第17行


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

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

它的工作原理。它显示了区域的总数。但是我需要计数选民任何想法如何实现?



更新

  //省.php 

<?php

命名空间Project\Bundle\DuterteBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;

/ **
*省
* /
类省
{
/ **
* @var整数
* /
私人$ id;

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

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

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

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

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

/ **
* @var整数
* /
private $ regionId;

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

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

/ **
*设定名称
*
* @param string $ name
* @return省
* /
public function setName($ name)
{
$ this-> name = $ name;

return $ this;
}

/ **
*获取名称
*
* @return string
* /
public function getName )
{
return $ this-> name;
}

/ **
*设置createdBy
*
* @param string $ createdBy
* @return省
* /
public function setCreatedBy($ createdBy)
{
$ this-> createdBy = $ createdBy;

return $ this;
}

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

/ **
*设置dateCreated
*
* @param \DateTime $ dateCreated
* @return省
* /
public function setDateCreated($ dateCreated)
{
$ this-> dateCreated = $ dateCreated;

return $ this;
}

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

/ **
*设置更新B
*
* @param string $ updatedBy
* @return省
* /
public function setUpdatedBy($ updatedBy)
{
$ this-> updatedBy = $ updatedBy;

return $ this;
}

/ **
*获取更新
*
* @return string
* /
public function getUpdatedBy )
{
return $ this-> updatedBy;
}

/ **
*设置dateUpdated
*
* @param \DateTime $ dateUpdated
* @return省
* /
public function setDateUpdated($ dateUpdated)
{
$ this-> dateUpdated = $ dateUpdated;

return $ this;
}

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

/ **
*设置regionId
*
* @param整数$ regionId
* @return省
* /
public function setRegionId($ regionId)
{
$ this-> regionId = $ regionId;

return $ this;
}

/ **
*获取regionId
*
* @return integer
* /
public function getRegionId )
{
return $ this-> regionId;
}
/ **
* @var \Doctrine\Common\Collections\Collection
* /
private $ city;

/ **
* @var \Project\Bundle\DuterteBundle\Entity\Region
* /
private $ region;

/ **
*构造函数
* /
public function __construct()
{
$ this-> city = new \\ \\Doctrine\Common\Collections\ArrayCollection();
}

/ **
*添加城市
*
* @param \Project\Bundle\DuterteBundle\Entity\City $ city
* @return省
* /
public function addCity(\Project\Bundle\DuterteBundle\Entity\City $ city)
{
$ this-> city [] = $ city;

return $ this;
}

/ **
*删除城市
*
* @param \Project\Bundle\DuterteBundle\Entity\City $ city
* /
public function removeCity(\Project\Bundle\DuterteBundle\Entity\City $ city)
{
$ this-> city- > removeElement($市);
}

/ **
*获取城市
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getCity()
{
return $ this-> city;
}

/ **
*设置区域
*
* @param \Project\Bundle\DuterteBundle\Entity\Region $ region
* @return省
* /
public function setRegion(\Project\Bundle\DuterteBundle\Entity\Region $ region = null)
{
$ this-> region = $ region;

return $ this;
}

/ **
*获取区域
*
* @return \Project\Bundle\DuterteBundle\Entity\Region
* /
public function getRegion()
{
return $ this-> region;
}
}

region.php

<?php

命名空间Project\Bundle\DuterteBundle\\ \\实体;

使用Doctrine\ORM\Mapping作为ORM;

/ **
*区域
* /
class区域
{
/ **
* @var integer
* /
private $ id;

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

/ **
* @var整数
* /
private $ islandId;

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

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

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

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

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

/ **
*设置名称
*
* @param string $ name
* @return区域
* /
public function setName($ name)
{
$ this-> name = $ name;

return $ this;
}

/ **
*获取名称
*
* @return string
* /
public function getName )
{
return $ this-> name;
}

/ **
*设置islandId
*
* @param integer $ islandId
* @return区域
* /
public function setIslandId($ islandId)
{
$ this-> islandId = $ islandId;

return $ this;
}

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

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

return $ this;
}

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

/ **
*设置dateCreated
*
* @param \DateTime $ dateCreated
* @return区域
* /
public function setDateCreated($ dateCreated)
{
$ this-> dateCreated = $ dateCreated;

return $ this;
}

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

/ **
*设置更新B
*
* @param string $ updatedBy
* @return区域
* /
public function setUpdatedBy($ updatedBy)
{
$ this-> updatedBy = $ updatedBy;

return $ this;
}

/ **
*获取更新
*
* @return string
* /
public function getUpdatedBy )
{
return $ this-> updatedBy;
}

/ **
*设置dateUpdated
*
* @param \DateTime $ dateUpdated
* @return区域
* /
public function setDateUpdated($ dateUpdated)
{
$ this-> dateUpdated = $ dateUpdated;

return $ this;
}

/ **
*获取dateUpdated
*
* @return \DateTime
* /
public function getDateUpdated()
{
return $ this-> dateUpdated;
}
/ **
* @var \Doctrine\Common\Collections\Collection
* /
private $ province;

/ **
* @var \Project\Bundle\DuterteBundle\Entity\Island
* /
private $ island;

/ **
*构造函数
* /
public function __construct()
{
$ this->省= \\Doctrine\Common\Collections\ArrayCollection();
}

/ **
*添加省
*
* @param \Project\Bundle\DuterteBundle\Entity\Province $ province
* @return Region
* /
public function addProvince(\Project\Bundle\DuterteBundle\Entity\Province $ province)
{
$ this-> province [] = $ province;

return $ this;
}

/ **
*获取省
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getProvince()
{
return $ this-> province;
}

/ **
*设置岛
*
* @param \Project\Bundle\DuterteBundle\Entity\Island $ island
* @return Region
* /
public function setIsland(\Project\Bundle\DuterteBundle\Entity\Island $ island = null)
{
$ this-> island = $ island;

return $ this;
}

/ **
*获取岛
*
* @return \Project\Bundle\DuterteBundle\Entity\Island
* /
public function getIsland()
{
return $ this-> island;
}

/ **
*删除省
*
* @param \Project\Bundle\DuterteBundle\Entity\Province $ province
* /
public function removeProvince(\Project\Bundle\DuterteBundle\Entity\Province $ province)
{
$ this->省 - > removeElement($省);
}
}

更新



为什么错误说一个方法不存在,实际上它存在于entity.Also我没有在调试工具栏中的错误。它都表示映射是有效的



更新



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



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $%
< td> {{island.id}}< / td>
< td> {{island.name}}< / td>
< td>
{%for region.region%}
{%for region.province%}
{%for city在province.city%}
{{city。
{%endfor%}
{%endfor%}
{%endfor%}
< / td>
< / tr>
{%endfor%}



标识号4'OFW'提供正确的数字,但其余的表示奇怪的整数。我知道原因岛('OFW')只有一个地区,一个省,一个城市和许多选民在记录中,而其余的(棉兰老岛,吕宋岛,维沙as斯)在每个记录中都有许多地区,省,市。如何修复其他3个岛屿(棉兰老岛,维沙as岛,吕宋岛) OFW岛?



//更新



我放弃了上一个解决方案的循环,我试过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(\Doctrine\ORM\NoResultException $ e){
return null;
}
}

但是当我尝试转储岛时显示为空

  {{dump(islands)}} //返回null 
{%为岛屿上的岛屿%}
< tr>
< td> {{island.id}}< / td>
< td> {{island.name}}< / td>
< td> {{island.region.province.city.voters | length}}< / td>
< / tr>
{%endfor%}


解决方案


DuterteBundle中不存在对象Doctrine\ORM\PersistentCollection的方法province:选民:islands.html.twig第17行


我认为第17行是那个

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

错误显然告诉你,属性实体 Island 中的区域不是单个实体,而是实体的集合;您应该检查您的 Island 类定义。


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,
    ));
}

I tried this in template

<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 %}

The error is

Method "province" for object "Doctrine\ORM\PersistentCollection" 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?

Update

//province.php

<?php

namespace Project\Bundle\DuterteBundle\Entity;

use Doctrine\ORM\Mapping 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 \Doctrine\Common\Collections\Collection
 */
private $city;

/**
 * @var \Project\Bundle\DuterteBundle\Entity\Region
 */
private $region;

/**
 * Constructor
 */
public function __construct()
{
    $this->city = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add city
 *
 * @param \Project\Bundle\DuterteBundle\Entity\City $city
 * @return Province
 */
public function addCity(\Project\Bundle\DuterteBundle\Entity\City $city)
{
    $this->city[] = $city;

    return $this;
}

/**
 * Remove city
 *
 * @param \Project\Bundle\DuterteBundle\Entity\City $city
 */
public function removeCity(\Project\Bundle\DuterteBundle\Entity\City $city)
{
    $this->city->removeElement($city);
}

/**
 * Get city
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getCity()
{
    return $this->city;
}

/**
 * Set region
 *
 * @param \Project\Bundle\DuterteBundle\Entity\Region $region
 * @return Province
 */
public function setRegion(\Project\Bundle\DuterteBundle\Entity\Region $region = null)
{
    $this->region = $region;

    return $this;
}

/**
 * Get region
 *
 * @return \Project\Bundle\DuterteBundle\Entity\Region 
 */
public function getRegion()
{
    return $this->region;
}
}

region.php

<?php

namespace Project\Bundle\DuterteBundle\Entity;

use Doctrine\ORM\Mapping 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 \Doctrine\Common\Collections\Collection
 */
private $province;

/**
 * @var \Project\Bundle\DuterteBundle\Entity\Island
 */
private $island;

/**
 * Constructor
 */
public function __construct()
{
    $this->province = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add province
 *
 * @param \Project\Bundle\DuterteBundle\Entity\Province $province
 * @return Region
 */
public function addProvince(\Project\Bundle\DuterteBundle\Entity\Province $province)
{
    $this->province[] = $province;

    return $this;
}

/**
 * Get province
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getProvince()
{
    return $this->province;
}

/**
 * Set island
 *
 * @param \Project\Bundle\DuterteBundle\Entity\Island $island
 * @return Region
 */
public function setIsland(\Project\Bundle\DuterteBundle\Entity\Island $island = null)
{
    $this->island = $island;

    return $this;
}

/**
 * Get island
 *
 * @return \Project\Bundle\DuterteBundle\Entity\Island 
 */
public function getIsland()
{
    return $this->island;
}

/**
 * Remove province
 *
 * @param \Project\Bundle\DuterteBundle\Entity\Province $province
 */
public function removeProvince(\Project\Bundle\DuterteBundle\Entity\Province $province)
{
    $this->province->removeElement($province);
}
}

update

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

update

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 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?

//update

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 (\Doctrine\ORM\NoResultException $e) {
    return 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 %}

解决方案

Method "province" for object "Doctrine\ORM\PersistentCollection" does not exist in DuterteBundle:Voters:islands.html.twig at line 17

I think the line 17 is that one

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

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天全站免登陆