php - cakephp find all posts that have comments -


i created simple blog has posts , comments. want find posts have @ least 1 comment , find posts no comments. there cakephp way this? i.e. maybe like

$this->post->find('all', ???); 

i ended writing own query, example below finds posts @ least 1 comment

select *   (        select posts.*, count(comments.id) comment_count         posts left join comments on posts.id = comments.post_id         group posts.id        ) t  comment_count != 0 

but there seems there better way this.

note: post hasmany comment , comment belongsto post

$grouped_comments = $this->comment->find('all', array('group' => 'comment.post_id')); 

this give array of comments grouped post_id have 1 comment each post, want. there can whatever want data.

let's wanted post list of post titles comments.

echo "<h1>posts comments:</h1>"; foreach ($grouped_comments $comment) { echo $comment['post']['title'] . "<br>"; } 

this of course works if have model relationships set in comment.php model.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -