计算关系表条目

时间:2012-10-30 11:05:46

标签: php mysql yii

代码:

$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);

响应:

[{"id":"1","name":"Profile 1","event_relational":"0"}]

代码2:

$criteria = new CDbCriteria;
$criteria->select='id, name';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);

回应2:

[{"id":"1","name":"Profile 1","event_relational":null},{"id":"2","name":"Profile 2","event_relational":null}...]


当我使用COUNT时,查询在第一个MySQL条目中停止。 我想代码返回所有条目并检查是否有关系事件。

我想要的回应:

[{"id":"1","name":"Profile 1","event_relational":"0"},{"id":"2","name":"Profile 2","event_relational":"1"}...]

1 个答案:

答案 0 :(得分:2)

要执行此操作,您需要group的{​​{1}}:

profile.id