Yii框架 - 使用连接的CDbCriteria

时间:2013-11-09 03:55:39

标签: php yii

我想在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;
        }

顺便生成模型。

但我不知道我想念的是什么。 有什么可以帮忙吗? 非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

默认情况下表中的

列的别名为 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;
    }