Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带子查询的Yii CListView搜索_Php_Sql_Yii - Fatal编程技术网

Php 带子查询的Yii CListView搜索

Php 带子查询的Yii CListView搜索,php,sql,yii,Php,Sql,Yii,我正在使用CListView在页面上显示我的数据。我有一个文本框用于搜索查询中的关键字 在构建CActiveDataProvider的查询中,我有一些子查询。例如: $criteria = new CDbCriteria; $criteria->select = array(' Lessons.id, Lessons.name, (SELECT COALESCE(CONCAT(u.first_name, " ", u.last_name), last

我正在使用CListView在页面上显示我的数据。我有一个文本框用于搜索查询中的关键字

在构建CActiveDataProvider的查询中,我有一些子查询。例如:

$criteria = new CDbCriteria;
$criteria->select = array('
    Lessons.id,
    Lessons.name,
    (SELECT
        COALESCE(CONCAT(u.first_name, " ", u.last_name), last_name, first_name)
    FROM
        users AS u
    WHERE
        u.user_token = Lessons.instructor_id
    ) as instructor_name
');
我的上述查询模型确实有一个名为$讲师_name的类变量

当我在文本框中输入数据时,我会运行这段代码连接另一个表进行搜索

if ( !empty($query) ) {
    $criteria->with = array('packages');
    $criteria->compare( 'packages.contents', $query, true);
    $criteria->together = true;
}
运行搜索查询时的结果不会从子查询返回讲师姓名数据


关于这里发生的阻止子查询数据加载的事情有什么想法吗?先谢谢你

要以更快捷的记录方式执行此操作(并获得您想要的结果),请尝试以下操作:

1-将讲师关系添加到引用用户表的课程模型中(这样现在您就可以执行$LESSION->TICHER)

2-在Lesson::search()方法的select子句中添加一列/表达式,该列/表达式表示连接的讲师名称-类似于:

$criteria->select = array('
Lessons.id,
Lessons.name,
COALESCE(CONCAT(instructor.first_name, " ", instructor.last_name), last_name, first_name) AS instructor_name
...etc
3-将讲师添加到搜索条件的“with”部分,以便将讲师信息加入到查询中

$criteria->with = array('packages', 'instructor');

我发现的似乎是当我使用搜索条件的一部分向添加值时。e、 g.
$criteria->with=array('讲师')运行的我的SQL查询不考虑原始select值。这样,您就不需要子查询了,因为列被连接在一起,然后使用公式来计算讲师姓名字段。
$criteria->with = array('packages', 'instructor');