H
H
hollanditkzn2018-04-05 15:08:55
Yii
hollanditkzn, 2018-04-05 15:08:55

How to return an array from activeRecord to a view?

It turns out that when I return a value from ActiveRecord in the form of json_encode I get an empty value, if so, as I did, then it's normal.

public function actionPagination($id, $offset)
    {
        $offset = $offset * 6;
        $arr = [];
        $comment = Comment::find()->where(['id_zakaz' => $id])->orderBy('id DESC')->offset($offset)->limit(6)->all();
        foreach ($comment as $key => $com){
            $arr[$key]['comment'] = $com->comment;
            $arr[$key]['date'] = $com->date;
            $arr[$key]['user_id'] = $com->id_user;
        }
        return json_encode($arr, JSON_UNESCAPED_UNICODE);
    }

Only it turns out too much code, but is there some code that is simple and without foreach?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2018-04-05
@hollanditkzn

$comment = Comment::find()->where(['id_zakaz' => $id])->orderBy('id DESC')->offset($offset)->limit(6)->asArray()->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question