0

In controller

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

in Twig i need custom query value.getCount to count rows

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

in Edition entity i add custom function:

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

But this doesn't work! :( Please tell me how i can do that.

3
  • Where is the issue ? Commented Apr 30, 2015 at 9:18
  • He's missing the brackets on the method call. Commented Apr 30, 2015 at 9:19
  • I think it's wrong to make a query inside an entity. For me you must do {{value.advertEdition|length}} or create a custom method inside your entityRepository. Commented Apr 30, 2015 at 9:46

2 Answers 2

1

If you don't have the "count" property in your object, then:

{{ value.getCount() }}

or if you have the count property and "getCount" is the only getter:

{{ value.count }}
Sign up to request clarification or add additional context in comments.

1 Comment

{{value.count}} try to make $value->count, $value->getCount() and $value->isCount().
0

For this case i found another solution, i created twig extension Count

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

And in twig:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.