Symfony - 从自定义查询中获取对象

时间:2017-11-28 23:33:48

标签: php symfony query-builder

我尝试获取一个名为" Entreprise"的对象数组。

这是我的自定义查询(在EntrepriseRepository.php中):

    $qb = $this
    ->createQueryBuilder('p')
    ->select('p.nom,p.adresse,p.ville,p.latitude,p.longitude,p.photo')
    ->addSelect('(6371 * acos(cos(radians(:lat)) * cos(radians(p.latitude)) * cos(radians(p.longitude) - radians(:lng)) + sin(radians(:lat)) * sin(radians(p.latitude)))) AS distance')
    ->having('distance <= 50')
    ->setParameter('lat', $lat) 
    ->setParameter('lng', $lng) ;

   // return $qb->getQuery()->getResult();
    return $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);

我的实体与#34; Disponibilite&#34;和&#34; Entreprise&#34;:

Disponibilite.orm.yml:

manyToOne:
    est_propose_par:
        targetEntity: Entreprise 
        inversedBy: propose

Entreprise.orm.yml:

oneToMany:
    propose:
        targetEntity: Disponibilite 
        mappedBy: est_propose_par

我想得到任何&#34; Disponibilite&#34;我的&#34; Entreprise&#34; :

{% for entreprise in entreprises %}
 {% for disponibilite in entreprise.propose %}
  <td>{{ disponibilite.dateDispo|date('H:i') }}</td>
 {% endfor %}
{% endfor %}

这里的错误:

Key "propose" for array with keys "nom, adresse, ville, latitude, longitude, photo, distance" does not exist.

我认为这是因为我没有归还整个对象&#34;企业&#34;但我不知道如何修改我的自定义请求。感谢您的帮助

编辑:

有了这次更新:

->select ('p');

我的控制器:

 $entreprises = $repositoryEntreprise->selectEntreprises($latitude,$longitude);
      return $this->render('CalandmeBundle:Default:recherche.html.twig',array('adresse' => $adresse,'latitude'=>$latitude,
      'longitude'=>$longitude,'entreprises'=>$entreprises));

我明白了:

'entreprises' => array(array(object(Entreprise), 'distance' => '0.6023921078894118')

但我无法使用该对象&#39; Entreprise&#39;:

Key "photo" for array with keys "0, distance" does not exist.

这是我在Twig {{ dump (entreprises[3][0] ) }}中写的结果:

Entreprise {#364 ▼
  -id: 12
  -nom: "Ainsi Soit Tif"
  -adresse: "47 Mail Albert 1er"
  -ville: "Amiens"
  -codePostal: 80000
  -description: "..."
  -est_possede_par: Professionnel {#369 ▶}
  -rendez_vous: PersistentCollection {#365 ▶}
  -pratique: PersistentCollection {#371 ▶}
  -est_pratique_par: null
  -latitude: 49.888718
  -longitude: 2.29876
  -photo: "img/coiffeur4.jpg"
  +propose: PersistentCollection {#367 ▶}
}

所以我试着获得第一个&#34; Entreprise&#34;的所有信息。 :

{% for entreprise in entreprises[0][0] %}

但它不起作用..

如果我用Twig写作:

{{ entreprises[0][0].nom }}

它有效

如果我用Twig写作:

   {% for disponibilite in entreprises[0][0].propose %}
<td> {{ disponibilite.dateDispo|date('Y-m-d')}} </td>
   {% endfor %}

它也有效。

但仅仅是第一个企业&#39;为什么我不能使用??:

{% for entreprise in entreprises[0][0] %} 

此处结果为dump(entreprises):

array:4 [▼
  0 => array:8 [▼
    0 => Entreprise {#289 ▶}
    "distance" => "0.6023921078894118"
    "photo" => "img/coiffeur.jpg"
    "nom" => "Chambre Air"
    "adresse" => "33 Rue St Leu"
    "ville" => "Amiens"
    "latitude" => 49.8985177
    "longitude" => 2.3005478
  ]
  1 => array:8 [▼
    0 => Entreprise {#335 ▶}
    "distance" => "0.49092062431986955"
    "photo" => "img/coiffeur2.jpg"
    "nom" => "Atmosp Hair"
    "adresse" => "25 Rue Vanmarcke"
    "ville" => "Amiens"
    "latitude" => 49.896528
    "longitude" => 2.301443
  ]
  2 => array:8 [▼
    0 => Entreprise {#356 ▶}
    "distance" => "0.18742302911019457"
    "photo" => "img/coiffeur3.jpg"
    "nom" => "Sam la Coup"
    "adresse" => "8 Rue du Général Leclerc"
    "ville" => "Amiens"
    "latitude" => 49.894634
    "longitude" => 2.293289
  ]
  3 => array:8 [▼
    0 => Entreprise {#364 ▶}
    "distance" => "0.6325871662424426"
    "photo" => "img/coiffeur4.jpg"
    "nom" => "Ainsi Soit Tif"
    "adresse" => "47 Mail Albert 1er"
    "ville" => "Amiens"
    "latitude" => 49.888718
    "longitude" => 2.29876
  ]
]

3 个答案:

答案 0 :(得分:0)

更新您的选择语句。

->select('p')

在twig尝试使用1D循环访问

{% for disponibilite in entreprise.propose %}
    <td>{{ disponibilite.dateDispo|date('H:i') }}</td>
{% endfor %}

如果它不起作用那么

{% for disponibilite in entreprise[0].propose %}
    <td>{{ disponibilite.dateDispo|date('H:i') }}</td>
{% endfor %}

或返回您的结果

$result =  $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);

return $result[0];

答案 1 :(得分:0)

根据我的理解,您需要Disponibilite的所有Enterprise。如果是这样,那么为什么不在自定义查询中使用连接?像:

//EntrepriseRepository.php
$qb = $this
    ->createQueryBuilder('p')
    ->leftJoin('p.propose', 'd')
    ->select('p.nom,p.adresse,p.ville,p.latitude,p.longitude,p.photo, d.id //ADD OTHER FIELDS HERE TO BE SELECTED FROM Disponibilite ENTITY')
    ->addSelect('(6371 * acos(cos(radians(:lat)) * cos(radians(p.latitude)) * cos(radians(p.longitude) - radians(:lng)) + sin(radians(:lat)) * sin(radians(p.latitude)))) AS distance')
    ->having('distance <= 50')
    ->setParameter('lat', $lat) 
    ->setParameter('lng', $lng)
;

return $qb->getQuery()->getResult(); //I THINK THAT IF YOU USE JUST ->getResult() IT WILL RETURN AN ARRAY OF OBJECTS BY DEFAULT. AND ONLY IF YOU WANT TO RETURN AN ARRAY OF ARRAYS THEN USE ->getArrayResult()

然后,您可以使用{{ dump(entreprises) }}查看该数组中的内容,然后了解如何创建迭代以显示Disponibilite

顺便说一下,entreprise[0]看起来很难看。

答案 2 :(得分:0)

好吧,我只需要使用它:

 {% for entreprise in entreprises %}

并呼吁任何参数:

 {{ entreprises[0].nom }} 

并且:

{% for disponibilite in entreprise[0].propose %}
<td> {{ disponibilite.dateDispo|date('HH:mm') }} </td>
{% endfor %}