2
class Retailjob extends CFormModel {

    public function getReatilProducts() {

        $condition=false;
        $user = Yii::app()->db->createCommand()
            ->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
            ->from('tbl_retailjob')
            ->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
            ->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id')           
            ->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7))
            ->queryAll();
        return $user;
    }
}

这是我的模型文件,我想要实现的是如果 $condition 为真,则应避免 where 条件,如果为假,则应包括在内

我可以这样实现吗

public function getReatilProducts() {

    $condition=true;
    $user = Yii::app()->db->createCommand()
        ->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
        ->from('tbl_retailjob')
        ->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
        ->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id')
    if ($condition !=true) {
        ->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7))
    }
    ->queryAll();
    return $user;   
}
}
4

1 回答 1

2

试试这个:

public function getReatilProducts() {    
    $condition=true;
    $command = Yii::app()->db->createCommand()
        ->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
        ->from('tbl_retailjob')
        ->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
        ->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id');
    if ($condition !=true) {
        $command->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7));
    }
    $user = $command->queryAll();
    return $user;   
}
于 2013-08-15T16:58:24.893 回答