Ren
Ren

Reputation: 49

Model vs active records

I am a newbie in YII2 and started learning this on my own. Recently I was working on YII2 models. While learning some pre built model files I noticed that some model files are extends through yii\base\Model where as some from \yii\db\ActiveRecord.
So I want to know the reason when one should use active records and when model i.e. we can do the query in model too so why is there need of active records. Which are the tasks can be done by active records but not by models. In simple words I want to know specific work of both model and active records.

Upvotes: 2

Views: 1800

Answers (2)

t6nnp6nn
t6nnp6nn

Reputation: 318

ActiveRecord is a Model that uses a database engine to store the model(s) data.

yii\base\Model is a Model that does not specify how the data is being stored.

Eg a Model could be without a data table and have the actual data stored inside the class code instead of a database. A good example is in the yii2-basic app - the User model here.

I has the data stored in the class code as:

private static $users = [
    '100' => [
        'id' => '100',
        'username' => 'admin',
        'password' => 'admin',
        'authKey' => 'test100key',
        'accessToken' => '100-token',
    ],
    '101' => [
        'id' => '101',
        'username' => 'demo',
        'password' => 'demo',
        'authKey' => 'test101key',
        'accessToken' => '101-token',
    ],
];

Upvotes: 4

uldis
uldis

Reputation: 362

Active records use for working with data tables and forms, but yii\base\Models for only for forms.

Upvotes: -1

Related Questions