denys281
denys281

Reputation: 2014

symfony count user

I use symfony 1.4.12 with doctrine. I have sfGuardUser table; I can count all records like this:

 $count_user=Doctrine::getTable('sfGuardUser')->count();

I have field in table "created_at"; If it possible to count all users, that where created in current year? Thank you!

Upvotes: 2

Views: 4517

Answers (2)

4levels
4levels

Reputation: 3234

There are various ways to get the count of a record collection. Doctrine automatically creates a count query for you when you call the ->count() function on a query object or Table instance.

If for instance you would like to know the total amount of users and the amount of users created in the current year, you could go about it as follows:

$query = Doctrine_Query::create()
  ->from('sfGuardUser');
// alternative query retrieval
// $query = sfGuardUserTable::getInstance()->createQuery();
// all users
$totalUsers = $query->count();
// users created this year
$query->where('YEAR(created_at) = ?', date('Y'));
$totalUsersThisYear = $query->count();

Upvotes: 1

Tom
Tom

Reputation: 30698

$first_day_of_current_year = date('Y-m-d H:i:s', strtotime('1 january'));

$q = Doctrine_Query::create()
  ->select('COUNT(u.*)')
  ->from('sfGuardUser u')
  ->where('u.created_at > ?', $first_day_of_current_year);
$total = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
$total = $total[0][0];

Upvotes: 3

Related Questions