Yii框架- CDbCriteria使用连接

a0x5cqrl  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(199)

我想在Yii中执行一个join查询。但是发生的是我得到一个内部服务器错误。我想执行一个按标签组搜索项目的查询。
下面是我想用yii执行的查询:

select i.id as id, i.title as title, i.content as content from tbl_tags t
    join tbl_items i on t.item_id = i.id
    where t.tag_group_id = (value) and i.item_id = (value)
    group by t.ref_id;

这是我在Yii中使用CDbCriteria的方法:

public function getItemsListByTagGroup( . . .){

            $criteria = new CDbCriteria;
            $criteria->select = 'i.id as id, i.title as title, i.content as content i.date_updated as date_updated';
            $criteria->join = 'join tbl_tag t on i.id = t.item_id';
            $criteria -> addCondition('t.tag_group_id = ' . $tag_group_id);
            $criteria -> addCondition('t.reference_type = ' . $ref_type);
            $criteria -> addCondition('t.tag_group_id = ' . $id);
            $criteria -> addCondition('i.item_type = ' . $item_type);
            $criteria -> addCondition('i.item_id  = ' .$item_id);
            $criteria -> addCondition('i.status = "active"');

            $item_list = Items::model() -> findAll($criteria);

            $item_list_data = array();
            foreach ($templates_list as $listdata => $data) {
                $columns = array();
                $columns['Title'] = $data['title'];
                $columns['Content'] = $data['content'];
                $columns['Date Updated'] = $data['date_updated'];
                $columns['id'] = $data['id'];

                $item_list_data[] = $columns;
            }
            return $item_list_data;
        }

顺便生成模型。
但我不知道我错过了什么或什么。有什么你能帮忙的吗?任何想法都是非常感谢。

drkbr07n

drkbr07n1#

默认情况下,Items表中列将具有t别名,因此,对于指向id列,您应该t.id,t.title,...,最后,您应该指定连接类型,
我建议使用第一种方法:

$result = Yii::app()->db->createCommand()
    ->select('i.id as id, i.title as title, i.content as content')
    ->from('tbl_tags t')
    ->join('tbl_items i' , 't.item_id = i.id')
    ->where('t.tag_group_id = :value1' , array(':value1' => $value1))
    ->andWhere('i.item_id = :value2' , array('value2' => $value2))
    ->group('t.ref_id')->queryAll(); // this will be returned as an array of arrays

第二个
,而当你用活动记录获取数据时,你不能把数据拿回来,因为另一列不在模型的属性列表中,试试这个

public function getItemsListByTagGroup( . . .){

        $criteria = new CDbCriteria;
        $criteria->select = 'i.id , i.title , i.content , i.date_updated'; // if these columns are not in model, won't get fetched
        $criteria->join = 'INNER JOIN tbl_items i on i.id = t.item_id'; // incorrect table join was in here
        $criteria -> addCondition('t.tag_group_id = ' . $tag_group_id);
        $criteria -> addCondition('t.reference_type = ' . $ref_type);
        $criteria -> addCondition('t.tag_group_id = ' . $id);
        $criteria -> addCondition('i.item_type = ' . $item_type);
        $criteria -> addCondition('i.item_id  = ' .$item_id);
        $criteria -> addCondition('i.status = "active"');

        $item_list = Items::model() -> findAll($criteria); // this will be fetched as an array of objects

        $item_list_data = array();
        foreach ($templates_list as $listdata => $data) { 
            $columns = array();
            $columns['Title'] = $data->title;
            $columns['Content'] = $data->content;
            $columns['Date Updated'] = $data->date_updated;
            $columns['id'] = $data->id;

            $item_list_data[] = $columns;
        }
        return $item_list_data;
    }

相关问题