我在SQL中有请求:
SELECT count(distinct b.id), count(distinct e.id) FROM building, ensembles e, `parcs` p WHERE p.id = "1" AND e.parcs_id = p.id AND e.id = b.ensembles_id
这是我在控制器ManageController.php
的 Symfony项目中使用此请求的方式:
class ManageController extends Controller
{
public function indexAction()
{
$em=$this->getDoctrine()->getManager();
$parc = $em->getRepository('GirDatabaseBundle:Parcs');
$ensemble = $em->getRepository('GirDatabaseBundle:Ensembles');
$building = $em->getRepository('GirDatabaseBundle:Buildings');
$query = $em->createQuery(
'SELECT p.name, count(distinct b.id), count(distinct e.id)
FROM GirDatabaseBundle:Buildings b, GirDatabaseBundle:Ensembles e, GirDatabaseBundle:Parcs p
WHERE p.id in (:id)
AND e.parcs = p.id
AND e.id = b.ensembles
GROUP BY p.id')
->setParameter('id', '1');
$parc = $query->getResult();
return $this->render('GirManageParcsBundle:Parcs:manage.html.twig', array('parc' => $parc ));
}
但是,当我推出我的html.twig页面manage.html.twig
时,我有错误:
Key" name"对于带有键的数组" 1,2" GirGestionPatrimoinesBundle中不存在:Parcs:第35行的manage.html.twig
所以,这是我的树枝的代码:
{# CONTENT #}
<div class="panel-body">
<p class="text-justify">
<div class="jumbotron">
<h4><u>Situation actuelle</u></h4>
<p>
<h5>PARCS</h5>
{% for parcs in parc %}
<h4> {{ parcs.name}} </h4>
{% endfor %}
</p>
</div>
</p>
我认为,我没有使用好的方式来显示我想要的数据。你知道它有效吗?我想显示
这是Parcs类/实体代码: 使用Doctrine \ ORM \ Mapping作为ORM;
/**
* Parcs
*
* @ORM\Table(name="parcs")
* @ORM\Entity
*/
class Parcs
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=150, nullable=false)
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $name
* @return Parcs
*/
public function setName($name)
{
$this->nom = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
这是Ensembles类/实体代码:
/**
* Ensembles
*
* @ORM\Table(name="ensembles")
* @ORM\Entity
*/
class Ensembles
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="referenceClient", type="string", length=150, nullable=false)
*/
private $referenceclient;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set referenceclient
*
* @param string $referenceclient
* @return Ensembles
*/
public function setReferenceclient($referenceclient)
{
$this->referenceclient = $referenceclient;
return $this;
}
/**
* Get referenceclient
*
* @return string
*/
public function getReferenceclient()
{
return $this->referenceclient;
}
/**
* @ORM\ManyToOne(targetEntity="Gir\DatabaseBundle\Entity\Parcs")
* @ORM\JoinColumn(nullable=false)
*/
private $parcs;
/**
* Set parcs
*
* @param \Gir\DatabaseBundle\Entity\Parcs $parcs
* @return Ensembles
*/
public function setParcs(\Gir\DatabaseBundle\Entity\Parcs $parcs)
{
$this->parcs = $parcs;
return $this;
}
/**
* Get parcs
*
* @return \Gir\DatabaseBundle\Entity\Parcs
*/
public function getParcs()
{
return $this->parcs
这是最后一个,建筑物类/实体代码:
class Batiments
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="referenceBatiment", type="string", length=150, nullable=false)
*/
private $referencebatiment;
/**
* @ORM\ManyToOne(targetEntity="Gir\DatabaseBundle\Entity\Ensembles")
* @ORM\JoinColumn(nullable=false)
*/
private $ensembles;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set referencebatiment
*
* @param string $referencebatiment
* @return Batiments
*/
public function setReferencebatiment($referencebatiment)
{
$this->referencebatiment = $referencebatiment;
return $this;
}
/**
* Get referencebatiment
*
* @return string
*/
public function getReferencebatiment()
{
return $this->referencebatiment;
}
/**
* Set ensembles
*
* @param \Gir\DatabaseBundle\Entity\Ensembles $ensembles
* @return Batiments
*/
public function setEnsembles(\Gir\DatabaseBundle\Entity\Ensembles $ensembles)
{
$this->ensembles = $ensembles;
return $this;
}
/**
* Get ensembles
*
* @return \Gir\DatabaseBundle\Entity\Ensembles
*/
public function getEnsembles()
{
return $this->ensembles;
}
回答riska的评论:
对于{{ dump(parc) }}
,它会返回我:
array (size=1)
0 =>
array (size=2)
'bcnt' => string '307' (length=3)
'ecnt' => string '104' (length=3)
和{{ dump(parcs) }}
,我有:
array (size=2)
'bcnt' => string '307' (length=3)
'ecnt' => string '104' (length=3)
答案 0 :(得分:2)
要避免此错误,请确保定义了在Twig中使用的变量,而不是null:
{# CONTENT #}
<div class="panel-body">
<p class="text-justify">
<div class="jumbotron">
<h4><u>Situation actuelle</u></h4>
<p>
<h5>PARCS</h5>
{% for parc in parcs %}
{% if parc.name is defined and parc.name is not null %}
<h4> {{ parcs.name}} </h4>
{% else %}
<h4> Parc without name</h4>
{% endif %}
{% endfor %}
</p>
</div>
</p>
顺便说一下,你的for循环中有一个拼写错误,看看上面的代码。
答案 1 :(得分:2)
我想你忘记了列的别名:
SELECT count(distinct b.id) AS bcnt, count(distinct e.id) AS ecnt
...
现在,您应该能够访问这些变量:
{% for parcs in parc %}
<h4> {{ parcs.bcnt }} </h4>
<h4> {{ parcs.ecnt }} </h4>
{% endfor %}