Active Record implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
Active Record implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
The idea is that an [[ActiveRecord]] object is associated with a row in a database table and its attributes are mapped
The premise behind Active Record is that an individual [[ActiveRecord]] object is associated with a specific row in a database table. The object's attributes are mapped to the columns of the corresponding table. Therefore, referencing an Active Record attribute is equivalent to accessing
to the columns of the corresponding table columns. Reading an ActiveRecord attribute is equivalent to accessing
the corresponding table column for that record.
the corresponding table column. For example, a `Customer` object is associated with a row in the
`tbl_customer` table, and its `name` attribute is mapped to the `name` column in the `tbl_customer` table.
To get the value of the `name` column in the table row, you can simply use the expression `$customer->name`,
just like reading an object property.
Instead of writing raw SQL statements to perform database queries, you can call intuitive methods provided
For example, say that the `Customer` ActiveRecord class is associated with the
by ActiveRecord to achieve the same goals. For example, calling [[ActiveRecord::save()|save()]] would
`tbl_customer` table. This would mean that the class's `name` attribute is automatically mapped to the `name` column in `tbl_customer`.
insert or update a row in the associated table of the ActiveRecord class:
Thanks to Active Record, assuming the variable `$customer` is an object of type `Customer`, to get the value of the `name` column for the table row, you can simply use the expression `$customer->name`. In this example, Active Record is providing an object-oriented way to access data stored in the database, just as you would access any object property. But Active Record provides much more functionality than this.
With Active Record, instead of writing raw SQL statements to perform database queries, you can call intuitive methods to achieve the same goals. For example, calling [[ActiveRecord::save()|save()]] would perform an INSERT or UPDATE query, creating or updating a row in the associated table of the ActiveRecord class:
```php
```php
$customer = new Customer();
$customer = new Customer();
@ -24,7 +22,7 @@ Declaring ActiveRecord Classes
------------------------------
------------------------------
To declare an ActiveRecord class you need to extend [[\yii\db\ActiveRecord]] and
To declare an ActiveRecord class you need to extend [[\yii\db\ActiveRecord]] and
implement the `tableName` method like the following:
implement the `tableName` method:
```php
```php
use yii\db\ActiveRecord;
use yii\db\ActiveRecord;
@ -41,13 +39,12 @@ class Customer extends ActiveRecord
}
}
```
```
Connecting to Database
Connecting to the Database
----------------------
----------------------
ActiveRecord relies on a [[Connection|DB connection]] to perform the underlying DB operations.
ActiveRecord relies on a [[Connection|DB connection]] to perform the underlying DB operations.
By default, it assumes that there is an application component named `db` which gives the needed
By default, ActiveRecord assumes that there is an application component named `db` which provides the needed
[[Connection]] instance. Usually this component is configured via application configuration
[[Connection]] instance. Usually this component is configured in application configuration file:
like the following:
```php
```php
return array(
return array(
@ -65,13 +62,10 @@ return array(
Please read the [Database basics](database-basics.md) section to learn more on how to configure
Please read the [Database basics](database-basics.md) section to learn more on how to configure
and use database connections.
and use database connections.
> Tip: To use a different database connection, you may override the [[ActiveRecord::getDb()]] method.
> Tip: To use a different database connection, you need to override the [[ActiveRecord::getDb()]] method. To do that, create a base class that extends ActiveRecord. In the base class, override the [[ActiveRecord::getDb()]] method. Then extend your new base class for all of your ActiveRecord classes that need to use the same alternative database connection.
You may create a base ActiveRecord class and override its [[ActiveRecord::getDb()]] method. You
then extend from this base class for all those ActiveRecord classes that need to use the same
DB connection.
Querying Data from Database
Querying Data from the Database
---------------------------
---------------------------
There are two ActiveRecord methods for querying data from database:
There are two ActiveRecord methods for querying data from database:
@ -79,8 +73,8 @@ There are two ActiveRecord methods for querying data from database:
- [[ActiveRecord::find()]]
- [[ActiveRecord::find()]]
- [[ActiveRecord::findBySql()]]
- [[ActiveRecord::findBySql()]]
They both return an [[ActiveQuery]] instance which extends from [[Query]] and thus supports
Both methods return an [[ActiveQuery]] instance, which extends from [[Query]] thus supporting
the same set of flexible and powerful DB query methods. The followings are some examples,
the same set of flexible and powerful DB query methods. The followings examples demonstrate some of the possibilities.
```php
```php
// to retrieve all *active* customers and order them by their ID:
// to retrieve all *active* customers and order them by their ID:
@ -122,10 +116,10 @@ Accessing Column Data
---------------------
---------------------
ActiveRecord maps each column of the corresponding database table row to an *attribute* in the ActiveRecord
ActiveRecord maps each column of the corresponding database table row to an *attribute* in the ActiveRecord
object. An attribute is like a regular object property whose name is the same as the corresponding column
object. The attribute behaves like any regular object public property. The attribute's name will be the same as the corresponding column
name and is case-sensitive.
name, and is case-sensitive.
To read the value of a column, you can use the following expression:
To read the value of a column, you can use the following syntax:
```php
```php
// "id" is the name of a column in the table associated with $customer ActiveRecord object
// "id" is the name of a column in the table associated with $customer ActiveRecord object