2

我有两个模型:

class Tag extends CActiveRecord {
    //// ......

    public function defaultScope(){ 
        return array(
            'condition' => 'class_name = :class_name',
            'params' => array(':class_name' => get_class($this))
       );
    }

    public function relations(){
        return array(
            'videos' => array(self::MANY_MANY, 'Video', 'tags_videos(tag_id, video_id)')
        );
    }

    //// ......
}

class Video extends CActiveRecord {
    //// .......

    public function relations(){
        return array(
            'tags' => array(self::MANY_MANY, 'Tag', 'tags_videos(video_id, tag_id)')
        );
    }

    //// .......
}

当我这样做时$video_instance->tags,我收到了这个错误:

 exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number. The SQL statement executed was: SELECT `tags`.`id` AS `t1_c0`, `tags`.`name` AS `t1_c1`, `tags`.`orig_name` AS `t1_c2`, `tags`.`slug` AS `t1_c3`, `tags`.`flags` AS `t1_c4`, `tags`.`class_name` AS `t1_c5`, `tags`.`created_at` AS `t1_c6` FROM `tags` `tags`  INNER JOIN `tags_videos` `tags_tags` ON (`tags_tags`.`video_id`=:ypl0) AND (`tags`.`id`=`tags_tags`.`tag_id`) AND (class_name = :class_name) WHERE (class_name = :class_name)' in /srv/http/yii-1.1.12.b600af/framework/db/CDbCommand.php:528

我的问题是:为什么我的默认范围两次应用于查询(在 WHERE 和 ON 中),我该如何解决这个问题?

Yii 版本:1.1.12.b600af PHP:5.4.8

4

1 回答 1

2

定义'scopes'=>array( 'resetScope' )关系

public function relations(){
        return array(
            'tags' => array(self::MANY_MANY, 'Tag', 'tags_videos(video_id, tag_id)','scopes'=>array( 'resetScope' ))
        );
}

最近有一个错误,我不确定它是否已在您的版本中修复: https ://github.com/yiisoft/yii/issues/1197

更多参考: http ://www.yiiframework.com/forum/index.php/topic/12025-relation-without-default-scope/

于 2012-10-23T14:44:00.767 回答