代码:
$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"}...]
答案 0 :(得分:2)
要执行此操作,您需要group
的{{1}}:
profile.id