Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. SELECT post.id, post.title, post.more_columns , COUNT(comments.post_id) AS numofcomments FROM post INNER JOIN comments ON post.id = comments.post_id GROUP BY post.id, post.title, post.more_columns
  2.  
  3. $con = Propel::getConnection(PostPeer::DATABASE_NAME);
  4.  
  5. $sql = "SELECT post.* , COUNT(comments.post_id) AS numcomments FROM post INNER JOIN comments ON post.id = comments.post_id GROUP BY post.id";
  6. $stmt = $con->prepare($sql);
  7. $stmt->execute();
  8.  
  9. $result = PostPeer::populateObjects($stmt);
  10. return $result;
  11.  
  12. class Post extends BasePost {
  13. protected $numComments;
  14.  
  15. public function setNumComments($v)
  16. {
  17. $this->numComments = $v;
  18. }
  19.  
  20. public function getNumComments()
  21. {
  22. return $this->numComments;
  23. }
  24. ...
  25. }
  26.  
  27. public static function doSelectWithCount() {
  28. $c = new Criteria();
  29. self::addSelectColumns($c); //add all columns from PostPeer
  30. $c->addAsColumn('numofcomments', 'COUNT('.CommentPeer::POST_ID.')');
  31. $c->addJoin(PostPeer::ID, CommentPeer::POST_ID, Criteria::LEFT_JOIN);
  32. $c->addGroupByColumn(PostPeer::ID);
  33. $c->addGroupByColumn(PostPeer::TITLE);
  34. // ...
  35. // more group-by columns if needed
  36. $rs = PostPeer::doSelectRS($c);
  37.  
  38. $posts = array();
  39. while ($rs->next()) {
  40. $post = new Post();
  41. $post->hydrate($rs);
  42. $post->setNumComments($rs->getInt(PostPeer::NUM_COLUMNS + 1));
  43. $posts[] = $post;
  44. }
  45.  
  46. return $posts;
  47. }
  48.  
  49. SELECT post.*,sub.numcomments from post,
  50. (select post.post_id as post_id,COUNT(comments.post_id) AS numcomments
  51. FROM post INNER JOIN comments ON post.id = comments.post_id GROUP BY post.id) as sub
  52. where post.post_id = sub.post_id;
  53.  
  54. $c = new Criteria();
  55. // count comments with
  56. $c->addAsColumn('numofcomments', 'COUNT('.CommentPeer::POST_ID.')');
  57. $c->addJoin(PostPeer::ID, CommentPeer::POST_ID, Criteria::LEFT_JOIN);
  58. $c->addGroupByColumn(PostPeer::ID);
  59. $c->addGroupByColumn(PostPeer::TITLE);
  60. // you can add more groupby column here
  61. //$c->addGroupByColumn(...more);
  62. $this->posts = PostPeer::doSelect($c);
  63.  
  64. //inside a static function in class PostPeer:
  65. $c = new Criteria();
  66. self::addSelectColumns($c); //add all columns from PostPeer
  67. $c->addJoin(PostPeer::ID, CommentPeer::POST_ID, Criteria::LEFT_JOIN);
  68. $cu->addAsColumn('numofcomments', 'COUNT('.CommentPeer::POST_ID.')');
  69. $cu->addGroupByColumn(PostPeer::ID);
  70. $cu->addGroupByColumn(PostPeer::TITLE);
  71. :
  72. :
  73. //more group-by columns if needed
  74. return PostPeer::doSelectStmt($c);
  75.  
  76. <div id="title">
  77. <?php echo post_array[1] ?>
  78. </div>
  79. //...and so on for other Post fields
Add Comment
Please, Sign In to add comment