2015-04-30 96 views
0

在控制器Symfony2的自定義查詢

return $this->render(
       'FrontBundle:Default:search.html.twig', 
       array(
        'edition'  => $edition,  THIS ONE 
        'paginator' => $pagination, 
        'array_ed_id' => $editions_search, 
        'array_he_id' => $heading_search, 
        'text'  => $text, 
        'seo'   => $seo, 
        'result'  => $result, 
        'testix'  => 10 
       ) 
      ); 
在枝杈

我需要自定義查詢value.getCount數行

{% for value in edition %} 
    <label><label class="" for="front_form_edition_s_{{ value.getId }}"> 
{{ value.getName }}</label>{{value.getCount}}</label> 
在版實體

我添加自定義功能:

use Doctrine\ORM\Query\ResultSetMapping; 
.......................... 
class Edition { 
.......................... 
    public function getCount() 
    { 

     $rsm = new ResultSetMapping(); 
     $query = $entityManager->createNativeQuery('select count(*) from advert_edition where edition_id= ?', $rsm); 
     $query->setParameter(1, '16'); 

     $users = $query->getResult(); 
     return 10; 
    } 
} 

但這不起作用! :(請告訴我,我怎麼能做到這一點

+0

問題在哪裏? – yunandtidus

+0

他缺少方法調用的方括號。 – pcm

+0

我認爲在實體內部進行查詢是錯誤的。對於我來說,你必須執行'{{value.advertEdition | length}}'或者在你的entityRepository中創建一個自定義方法。 –

回答

1

如果沒有「數」屬性在你的對象,那麼:

{{ value.getCount() }} 

,或者如果你有count屬性和「getCount將」是唯一的吸氣劑:

{{ value.count }} 
+0

'{{value.count}}'嘗試使'$ value-> count','$ value-> getCount()'和'$ value-> isCount()'。 –

0

對於這種情況,我發現另一種解決方案,我創建嫩枝擴展計數

public function countEdition($id) 
{ 
    $connection = mysql_connect("localhost", "root", "", "Test") or die("Could not connect in CountExt: " . mysql_error()); 
    mysql_select_db('Express',$connection)or die("Could not connect in CountExt: " . mysql_error()); 
    $query="select count(*) from advert_edition where edition_id=$id"; 
    $result = mysql_query($query) or die('Bad query CountExt: ' . mysql_error()); 
    $count=mysql_fetch_row($result); 
    return $count[0]; 
} 

並在樹枝上:

{% for value in edition %} 
<label><label class="" for="front_form_edition_s_{{ value.getId }}">{{ value.getName }}</label>{{value.getId|count}}</label> 
{% endfor %}