我正在尝试计算关系表中的行数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.