リレーショナル テーブルの行数をカウントしようとしてmany to many
いますが、常に間違った値が返されます。1 の場合は常に 2 を返します。
PS: mysql のすべてのモデルと外部キーは正しく構成されています。
コメント表:
id | name
10 Comment Test
ユーザー テーブル:
id | name
20 User Test
いいね (コメント/ユーザー) 多対多:
user_id | comment_id
20 10
コード:
$criteria = new CDbCriteria;
$criteria->select='*, COUNT(likes.id) AS count_likes'; // I believe the error is in the use of COUNT (likes.id).
$criteria->with=array('likes'=>array('on'=>'user_id=20'));
$model = Comments::model()->findByPk($_GET['id'], $criteria);
// Return Wrong Value
echo $model->count_likes; // Return 2 where should be 1. (I need to use this case)
echo count($model->likes); // Return right value 1.