开发者

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!


$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];


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜