doucang2871 2014-12-01 12:45
浏览 28
已采纳

GirManageParcsBundle中不存在具有键“1,2”的数组的键“name”:Parcs:manage.html.twig

I have this request in 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

This how I use this request in my Symfony project, in a controller ManageController.php :

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

But, when I lauched my html.twig page manage.html.twig, I have this error: Key "name" for array with keys "1, 2" does not exist in GirGestionPatrimoinesBundle:Parcs:manage.html.twig at line 35

So, here's the code for my 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>

I think, I don't use the good way to display the data like I want. Do you know it works ? And I would like to display the name of my parc and his number of ensembles and buildings that belong to him.

This is the Parcs class/entity code: use Doctrine\ORM\Mapping as 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;
    }

This is the Ensembles class/entity code:

 /**
 * 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

And this the last one, the buildings class/entity code:

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

To answer to riska's comment:

For {{ dump(parc) }}, it returns me:

array (size=1)
  0 => 
    array (size=2)
      'bcnt' => string '307' (length=3)
      'ecnt' => string '104' (length=3)

and for {{ dump(parcs) }}, I have:

array (size=2)
  'bcnt' => string '307' (length=3)
  'ecnt' => string '104' (length=3)
  • 写回答

2条回答 默认 最新

  • dtny30176 2014-12-01 15:07
    关注

    I think you forgot to alias the columns:

    SELECT count(distinct b.id) AS bcnt, count(distinct e.id) AS ecnt
    ...
    

    Now, you should be able to access to these variables:

    {% for parcs in parc %}
        <h4> {{ parcs.bcnt }} </h4>
        <h4> {{ parcs.ecnt }} </h4>
    {% endfor %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题