计算相关实体

时间:2015-08-12 17:21:10

标签: symfony doctrine-orm twig

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

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

错误是

  

对象“Doctrine \ ORM \ PersistentCollection”的方法“省”在DuterteBundle中不存在:Voters:islands.html.twig at the line 17

我试图删除像这样的实体

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

它有效。它显示了地区的总数。但我需要统计选民。任何想法如何实现这一目标?

更新

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

更新

为什么错误说方法不存在,实际上它存在于实体中。同样我在调试工具栏中没有错误。它都说映射是有效的

更新

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

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

enter image description here

Id号码4'OFW'呈现正确的数字,但其余的呈现奇怪的整数。我知道岛屿('OFW')只有一个地区,一个省,一个城市和许多选民在记录中的原因,而休息(棉兰老岛,吕宋岛,米沙鄢群岛)在每个记录中都有许多地区,省,市。如何修复其他3个岛屿(棉兰老岛,米沙鄢群岛,吕宋岛)以呈现像“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;
  }
}

但是当我试图'转储'岛

时显示为null
{{ 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 %}

1 个答案:

答案 0 :(得分:0)

  

对象“Doctrine \ ORM \ PersistentCollection”的方法“省”在DuterteBundle中不存在:Voters:islands.html.twig at the line 17

我认为第17行就是那个

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

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