-1

我正在尝试准备以下 sql 查询,但不幸的是它不起作用。

select course.course_name, enroll.start_date from dp_courses course LEFT OUTER JOIN dp_enroll enroll ON course.id = enroll.course_id and enroll.student_id=3

$criteria=new CDbCriteria(array(
            'order'=>'update_time DESC',
            'with'=> array('enroll'),
        ));
    $criteria->alias = 'course';
    $criteria->together=true;
    $criteria->join='LEFT OUTER JOIN dp_enroll ON dp_enroll.course_id=course.id and dp_enroll.student_id='. Yii::app()->user->id;
            //$criteria->condition='dp_enroll.student_id='. Yii::app()->user->id;

课程模型中的关系是:

 public function relations()    { 
    return array(
                    'enroll'=> array(self::HAS_MANY, 'Enroll', 'course_id'),
                    'author' => array(self::BELONGS_TO, 'YumProfile', 'author_id'),         );  }

但生成的 sql 查询我想要什么

LEFT OUTER JOIN dp_enroll enroll ON course.id = enroll.course_id **and** enroll.student_id=3

使用and运算符在两个属性上连接表

4

1 回答 1

1

它在文档中,第 1 点声明关系。Yii 在您的示例中没有关于复合连接的想法。如果你想像这样加入,你必须定义复合键关系,instadcourse_id用数组定义它,如文档中所述:

array('fk_c1'=>'pk_c1','fk_c2'=>'pk_c2')
于 2013-05-21T09:54:23.003 回答