{"id":1310,"date":"2022-08-30T15:15:28","date_gmt":"2022-08-30T15:15:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/how-to-use-distinct-in-zend-db-model-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:28","modified_gmt":"2022-08-30T15:15:28","slug":"how-to-use-distinct-in-zend-db-model-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-use-distinct-in-zend-db-model-collection-of-common-programming-errors\/","title":{"rendered":"How to use &#39;distinct&#39; in zend db model-Collection of common programming errors"},"content":{"rendered":"<p>You can specify mysql functions in the &#8216;from&#8217; function that makes up select query function. To use the from function you need to pass the table name as the first parameter, however passing $this (your table model class) works fine.<\/p>\n<pre><code>public function countFollowers($user_id)\n{\n  $rowset = $this-&gt;fetchAll(\n    $this-&gt;select()\n    -&gt;from($this, array('DISTINCT user_id'))\n    -&gt;where('user_id = ?', $user_id)\n  );\n\n  return count($rowset);\n}\n<\/code><\/pre>\n<p>[edit]<\/p>\n<p>Based on your edit, &#8216;group&#8217; may also work for you:<\/p>\n<pre><code>public function countFollowers($user_id)\n{\n  $rowset = $this-&gt;fetchAll(\n    $this-&gt;select()\n    -&gt;where('user_id = ?', $user_id)\n    -&gt;group('user_id')\n  );\n\n  return count($rowset);\n}\n<\/code><\/pre>\n<p>This will group all matching user_id into one record. So if a user is found, it will return 1, else 0.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:16:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>You can specify mysql functions in the &#8216;from&#8217; function that makes up select query function. To use the from function you need to pass the table name as the first parameter, however passing $this (your table model class) works fine. public function countFollowers($user_id) { $rowset = $this-&gt;fetchAll( $this-&gt;select() -&gt;from($this, array(&#8216;DISTINCT user_id&#8217;)) -&gt;where(&#8216;user_id = ?&#8217;, $user_id) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1310","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1310"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1310\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}