0
 $categories = Category::find()->where(['id' => $id])->all();

как этот запрос сунуть в ассоциативный массив со всеми полями

2 ответа 2

2

Вот так

$categories = Category::find()->where(['id' => $id])->asArray()->all();

Ссылка на метод asArray()

Это первый вариант.

Второй вариант использовать для этих целей ArrayHelper::toArray(). Будет что-то вроде этого

$categories = Category::find()->where(['id' => $id])->all();

$categoriesArray = ArrayHelper::toArray($categories, [
    'app\models\Category' => [
        'id',
        'title',
        // the key name in array result => property name
        'createTime' => 'created_at',
        // the key name in array result => anonymous function
        'length' => function ($post) {
            return strlen($post->content);
        },
    ],
])
1

Документация. Документация. Документация

$categories = Category::find()->where(['id' => $id])->asArray()->all();
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.