无法获取DISTINCT列值:

时间:2020-12-18 13:09:10

I am working in Yii and, I have a table named : visits

我在Yii工作,我有一张名为:visit的表

It has two foreign keys : from_user_id and to_user_id which are linked to the 'user' table. Now the table visits has many same 'from_user_id' and I want to get retrieve them in Yii as DISCTINCT.

它有两个外键:from_user_id和to_user_id,它们链接到'user'表。现在表访问有许多相同的'from_user_id',我想在Yii中将它们作为DISCTINCT进行检索。

This is my code :

这是我的代码:

$visited = Visit::model()->findAllByAttributes(array('to_user_id'=>Yii::app()->user->id));

$criteria = new CDbCriteria();
$criteria->distinct = true;

foreach($visited as $visits){

    echo User::model()->findByPk($visits->from_user_id,$criteria)->getFullName($visits->from_user_id);

   echo " <br>";
}

However, I am not able to get them as distinct.

但是,我无法将它们区分开来。

This is my output :

这是我的输出:

san
san
san
Leo
Leo

I want the output as :

我希望输出为:

San
Leo

This is not getting me distinct values. Where am I going wrong??

这不是让我有明显的价值观。我哪里错了?

1 个解决方案

#1


1  

I wasn't be able to test it, but apart from possible minor syntax adjustment, this should help you:

我无法测试它,但除了可能的小语法调整之外,这应该可以帮助您:

$visited = Visit::model()->findAllByAttributes(
    array('to_user_id'=>Yii::app()->user->id),
    array('distinct' => True)
);

According to the doc, findAllByAttribute accept a condition or criteria parameter as second argument.

根据doc,findAllByAttribute接受条件或条件参数作为第二个参数。

If this is an array (according the doc for find()), it will serve to initialize the various criteria properties of the requests.

如果这是一个数组(根据find()的文档),它将用于初始化请求的各种条件属性。

#1


1  

I wasn't be able to test it, but apart from possible minor syntax adjustment, this should help you:

我无法测试它,但除了可能的小语法调整之外,这应该可以帮助您:

$visited = Visit::model()->findAllByAttributes(
    array('to_user_id'=>Yii::app()->user->id),
    array('distinct' => True)
);

According to the doc, findAllByAttribute accept a condition or criteria parameter as second argument.

根据doc,findAllByAttribute接受条件或条件参数作为第二个参数。

If this is an array (according the doc for find()), it will serve to initialize the various criteria properties of the requests.

如果这是一个数组(根据find()的文档),它将用于初始化请求的各种条件属性。