实体orm中的Symfony2自定义查询

时间:2015-04-30 09:16:10

标签: php mysql symfony orm doctrine

在控制器中

 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
                )
            );
在Twig中

我需要自定义查询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;
    }
}

但这不起作用! :(请告诉我怎么做。

2 个答案:

答案 0 :(得分:1)

如果您的对象中没有“count”属性,则:

{{ value.getCount() }}

或者如果你有count属性,“getCount”是唯一的getter:

{{ value.count }}

答案 1 :(得分: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 %}