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 %}
I think the line 17 is that one
The error is clearly telling you that the property
region
inside the entityIsland
is not a single entity but a collection of entity; you should check yourIsland
class definition.