如何使用PHQL添加虚拟列

时间:2013-10-15 15:53:44

标签: php phalcon

如何使用模型查询对象添加虚拟列。这个不起作用

$user = User::query()
  ->columns(array(
    '*',
    '(SELECT COUNT(*) FROM `user`) AS `UserCnt`'
  ))
  ->execute();

1 个答案:

答案 0 :(得分:0)

你可以使用这样的计数:

// 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'");