Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'distinct' in zend db model

I have search for a long time to get this thing work.

What I want is to know how I user the 'distinct' in a zend db model to make my selection for the followers of a user unique.

My db model to count followers for a user (here I need to add the 'distinct')

public function countFollowers($user_id) 
{    
    $rowset       = $this->fetchAll("user_id = $user_id");
    $rowCount     = count($rowset);

    if ($rowCount > 0) {
      return $rowCount;
    } else {
      return $rowCount;
    }
}

EDIT: This function is part of 'class Application_Model_DbTable_Followers extends Zend_Db_Table_Abstract'

My table structure

  • id
  • article_id // Id of the article who is written by 'user_id'.
  • user_id // user_id owner of the article
  • follower_id // member who has following this article
  • date // date of follow

'user_id' can be written various articles, the follower can follow various articles of the same writer. I want to make a unique follower count. As an example what I want, If a follower is following 8 articles of one writer it has to be compared to '1' in the count.

I hope this will be clear enough to understand what I tried to reach.

With kind regards,

Nicky

like image 802
Nicky Avatar asked Jul 18 '11 12:07

Nicky


3 Answers

Using distinct:

public function countFollowers($user_id) 
{
    $select = $this->select()
              ->distinct()
              ->where('user_id = ?', $user_id);

    $rowset = $this->fetchAll($select);
    $rowCount = count($rowset);

    return $rowCount;
}

EDIT: After edit in question to get count of followers of a user. You actually need to use group NOT distinct. I have tested the following query works to fetch the data to be count()ed,

SELECT * FROM followers WHERE user_id = 1 GROUP BY user_id, follower_id

I have not tested the code, but something like this should work:

public function countFollowers($user_id) 
{
    $select = $this->select()
              ->where('user_id = ?', $user_id)
              ->group(array('user_id', 'follower_id')); 

    $rowset = $this->fetchAll($select);
    $rowCount = count($rowset);

    return $rowCount;
}
like image 116
Ozair Kafray Avatar answered Nov 15 '22 20:11

Ozair Kafray


Retrieving all the rows simply to get a count strikes me as overkill.

You can do a count using something like this:

$select = $db->select();
$select->from('testcount', new Zend_Db_Expr('COUNT(id)'))
       ->where('user_id = ?', $someUserId);
return $db->fetchOne($select);
like image 41
David Weinraub Avatar answered Nov 15 '22 19:11

David Weinraub


You can specify mysql functions in the 'from' 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->fetchAll(
    $this->select()
    ->from($this, array('DISTINCT user_id'))
    ->where('user_id = ?', $user_id)
  );

  return count($rowset);
}

[edit]

Based on your edit, 'group' may also work for you:

public function countFollowers($user_id)
{
  $rowset = $this->fetchAll(
    $this->select()
    ->where('user_id = ?', $user_id)
    ->group('user_id')
  );

  return count($rowset);
}

This will group all matching user_id into one record. So if a user is found, it will return 1, else 0.

like image 44
Richard Parnaby-King Avatar answered Nov 15 '22 20:11

Richard Parnaby-King