2

I have a database it looks like this:

Entity: Post, Comment.. Comment has a foreign key post_id.

I need to get Comments that belongs to specific post by its ID. And sort it by getCreated() metod.

Problem is, when i try to find all comments by post_id like:

$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBypost_id($postID); 

usort($comments, function($a, $b){
       return ($a->getCreated() < $b->getCreated());
    });

Im gettig ..CommonBundle\Entity\Comment' has no field 'postId'. Probably because its foreign key.

Is there any way, how to do it?

Thanks a lot!

2
  • You could try findBy(array('IDENTITY(comment)' => $postID)), although I'm not sure if you can use the IDENTITY function within findBy...so you might need to create a query builder instead. Commented Dec 28, 2014 at 20:10
  • thanks but, it gives "Unrecognized field: IDENTITY(Comment)" :/ Commented Dec 28, 2014 at 20:14

1 Answer 1

2

Seems you have an association like this:

/**
 * @ORM\ManyToOne(targetEntity="Post")
 */
protected $post;

So you should find your comments by post, not by post_id:

$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBy(['post' => $postID]);

And you can also sort it just by adding ['created' => 'DESC'] as a second parameter:

$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBy(['post' => $postID], ['created' => 'DESC']);
Sign up to request clarification or add additional context in comments.

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.