如何使用模型查询对象添加虚拟列。这个不起作用
$user = User::query()
->columns(array(
'*',
'(SELECT COUNT(*) FROM `user`) AS `UserCnt`'
))
->execute();
你可以像这样使用计数:
// How many cars are by each brand?
$phql = "SELECT Cars.brand_id, COUNT(*) FROM Cars GROUP BY Cars.brand_id";
$rows = $manager->executeQuery($phql);
foreach ($rows as $row) {
echo $row->brand_id, ' ', $row["1"], "\n";
}
或使用模型抽象:
$count = User::count();
或使用过滤器:
$count = User::count("status= 'active'");