Yii2 - 无法通过条件查询从模型接收数据

时间:2018-03-06 07:12:37

标签: yii2 yii2-model

我正在执行以下查询:

$model = ProjectSites::find()->alias('ps')->select(['ps.site_id', 'ps.id', 'c.circle_name', 'iss.issue', 'iss.created_at', 'iss.id'])
                    ->join('INNER JOIN', 'tbl_circle c', 'c.id=ps.circle_id')
                    ->join('INNER JOIN', 'tbl_site_issue iss', 'iss.site_id=ps.id')
                    ->where(['ps.delete_flag' => '0'])
                    ->andWhere("ps.circle_id in ($circle_ids)");
    if($project_ids) {
        $model->andWhere("ps.project_id in ($project_ids)");
    }
    if($date_range) {
        $date = explode('-', $date_range);
        $from_date = date('Y-m-d 00:00:00', strtotime($date[0]));
        $to_date = date('Y-m-d 23:59:59', strtotime($date[1]));
        $model->andWhere("ps.created_at >= '$from_date'");
        $model->andWhere("ps.created_at <= '$to_date'");
    }
    $model->orderBy(['c.id' => SORT_DESC]);
    $model->asArray()->all();
    var_dump($model); die;

当我生成SQL查询并在Phpmyadmin中执行时,它返回数据,但我执行上面的代码,我得到以下结果:

object(yii\db\ActiveQuery)#129 (28) { ["sql"]=> NULL ["on"]=> NULL ["joinWith"]=> NULL ["select"]=> array(6) { [0]=> string(10) "ps.site_id" [1]=> string(5) "ps.id" [2]=> string(13) "c.circle_name" [3]=> string(9) "iss.issue" [4]=> string(14) "iss.created_at" [5]=> string(6) "iss.id" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { ["ps"]=> string(17) "tbl_project_sites" } ["groupBy"]=> NULL ["join"]=> array(2) { [0]=> array(3) { [0]=> string(10) "INNER JOIN" [1]=> string(12) "tbl_circle c" [2]=> string(17) "c.id=ps.circle_id" } [1]=> array(3) { [0]=> string(10) "INNER JOIN" [1]=> string(18) "tbl_site_issue iss" [2]=> string(17) "iss.site_id=ps.id" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } ["where"]=> array(3) { [0]=> string(3) "and" [1]=> array(1) { ["ps.delete_flag"]=> string(1) "0" } [2]=> string(19) "ps.circle_id in (4)" } ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> array(1) { ["c.id"]=> int(3) } ["indexBy"]=> NULL ["emulateExecution"]=> bool(false) ["modelClass"]=> string(40) "app\modules\projects\models\ProjectSites" ["with"]=> NULL ["asArray"]=> bool(true) ["multiple"]=> NULL ["primaryModel"]=> NULL ["link"]=> NULL ["via"]=> NULL ["inverseOf"]=> NULL }

我想生成模型的结果,但我得到了一个查询对象。

感谢。

1 个答案:

答案 0 :(得分:1)

这样做:

$result = $model->asArray()->all();
var_dump($result); die;