M
M
MaoCzedun2015-06-09 22:47:32
Yii
MaoCzedun, 2015-06-09 22:47:32

Getting all records of a Yii2 table?

In general, the essence of the question is how to get all table records for dataProvider in Yii2, in Yii1 it was possible to do so findAll() of the model, and then give it to CArrayDataProvider , or by default CActiveDataProvider pulled all data from the table, how to do it in Yii2
Here is AR models

<?php

namespace app\models;
//   использовать    геттеры для  отоншений  между  объектами
class User extends \yii\db\ActiveRecord
{
    /*
     * @property integer $id
     * @property string $firstName
     * @property string $lastName
     * @property string $position
     * @property string $password
     * @property string $email
     */
//   Функция для  отношения с таблицей tasks 
//    public function getTasks()
//    {
//        
//    }
    public $captcha;
    public function scenarios() {
        return[
            'login'=>[
                'email','password'
            ],
//            insert - сценарий для  базы данных  
            'register'=>[
                'firstName','lastName','position','email','password'
            ],
            'edit'=>[
                'position'
            ],
            
        ];
    }
    public static function tableName() {
        return 'users';
    }
    public function rules() {
        return [
            [['firstName','lastName','position','email','password'],'required','on'=>'register'],
            [['email','password'],'required','on'=>'login'],
            [['email'],'email'],
            [['password'],'string','min'=>6,'max'=>32,'tooLong'=>'Пароль слишком длиный','tooShort'=>'Пароль слишком короткий'],
            [['captcha'],'required','on'=>'register'],
            [['captcha'],'captcha','on'=>'register'],
            
        ];
    }
    public function attributeLabels() {
        return [
            'firstName'=>'Имя',
            'lastName'=>'Фамилия',
            'email'=>'электронный  адресс',
            'password'=>'пароль',
            'position'=>'должность',
            'id'=>'id пользователя',
        ];
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
matperez, 2015-06-09
@matperez

I haven't tried it myself, but maybe it will work if you tell the provider the page size by the number of all possible records...

$count = User::find()->count();
$provider = new ActiveDataProvider([
            'query' => User::find(),
            'pagination' => [
                'pageSize' => $count,
            ],
]);

I
Igor Karachentsev, 2015-06-10
@kucheriavij

There are a hundred pounds here https://github.com/yiisoft/yii2/blob/master/docs/g...

L
LAV45, 2015-06-10
@LAV45

Ras

$users = new ArrayDataProvider([
    'allModels' => User::find()->asArray()->all(),
    'pagination' => false,
    'key' => 'id',
]);

Dvas
$users = new ActiveDataProvider([
    'query' => User::find(),
    'pagination' => false,
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question