2

I have two classes

    class Topic
    {
        protected $id;
        //....
    }

and

class Post
{
    protected $topic_id;
    //...
}

and I would like add method getPostCount() in Topic class. In other frameworks I used to use something like that:

 public function getPostCount()
    {        
            $count = Post::find()
                ->where(['topic_id' => $this->id])
                ->count();

        return $count;
    }

but in symfony2 I don't know how to make it.

4 Answers 4

3

You can create a repository class with this method. Add the repository class name to your entity's mapping definition, like this:

/**
*  @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
    protected $topic_id;
    //...
}

And in your repository class:

public function getPostCount($id)
{        
     $query = $this->createQueryBuilder('p')
        ->select('count(p.topic_id)')
        ->where('p.topic_id = :id')
        ->setParameter('id', $id)
        ->getQuery()->getSingleScalarResult();
    return $query; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

hello, Thank you for your answer. How can I use your solution to show postCount in view in twig? Somethig like post.getPostCount(topic.id) ?
If you only need one count, pass the variable from controller to twig. By other hand you can create a Twig filter, this is a little example ourcodeworld.com/articles/read/161/…
3

In addition to @DonCallisto answer

//Topic.php
public function getPostsCount()
{
    return $this->getPosts()->count();
}

This use doctrine lazyloading: it could be done because you already defined the relation between the entity.

It would not be a good practice to do a query inside the entity, you should use a Repository for that.

3 Comments

First, you're not explicitly doing a query: is doctrine that do this for you and is perfectly fine. Second: you can annotate the field to be hydrated every time the entity is loaded (but I'm sure you won't in this case as maybe postsCount is not needed every time). Only "bad practice" here is in performance possible issues.
@DonCallisto The query part is about the author example. its method getPostCount() is inside the entity (he uses $this->id). I have no problem with your answer
I was just explain something, no problem with your answer at all! :) Cheers
2
//Topic.php

public function getPostsCount()
{
    return $this->getPosts()->count();
}

If you have configured annotations or yml properly, you're fine with this

Comments

2

Into Post repository:

 public function getPostCount($id) {
    $qb = $this->getEntityManager()->createQueryBuilder();      
    $qb->select('count(p.topic_id)');
    $qb->from('AppBundle:Post', 't')
            ->where('p.topic_id = :id')
            ->setParameter('id', $id);
    $count = $qb->getQuery()->getSingleScalarResult();
    return $count;
}

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.