1

代码:

$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"}...]
4

1 回答 1

2

为此,您需要groupprofile.id

$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$criteria->group='t.id';
$model = Profiles::model()->findAll($criteria);
于 2012-10-30T11:29:22.643 回答