Browse Source

minor doc improvement.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
494fb8f695
  1. 31
      docs/guide/active-record.md

31
docs/guide/active-record.md

@ -3,7 +3,7 @@ Active Record
ActiveRecord implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record). ActiveRecord 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 so object properties are mapped The idea is that an ActiveRecord object is associated with a row in a database table so object properties are mapped
to colums of the corresponding database row. For example, a `Customer` object is associated with a row in the to columns of the corresponding database row. For example, a `Customer` object is associated with a row in the
`tbl_customer` table. `tbl_customer` table.
Instead of writing raw SQL statements to access the data in the table, you can call intuitive methods available in the Instead of writing raw SQL statements to access the data in the table, you can call intuitive methods available in the
@ -21,7 +21,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 `tableName` method like the following: implement the `tableName` method like the following:
```php ```php
class Customer extends \yii\db\ActiveRecord class Customer extends \yii\db\ActiveRecord
@ -39,10 +39,10 @@ class Customer extends \yii\db\ActiveRecord
Connecting to Database Connecting to Database
---------------------- ----------------------
ActiveRecord relies on a [[Connection|DB connection]]. By default, it assumes that ActiveRecord relies on a [[Connection|DB connection]] to perform the underlying DB operations.
there is an application component named `db` that gives the needed [[Connection]] By default, it assumes that there is an application component named `db` which gives the needed
instance which serves as the DB connection. Usually this component is configured [[Connection]] instance. Usually this component is configured via application configuration
via application configuration like the following: like the following:
```php ```php
return array( return array(
@ -52,26 +52,27 @@ return array(
'dsn' => 'mysql:host=localhost;dbname=testdb', 'dsn' => 'mysql:host=localhost;dbname=testdb',
'username' => 'demo', 'username' => 'demo',
'password' => 'demo', 'password' => 'demo',
// turn on schema caching to improve performance // turn on schema caching to improve performance in production mode
// 'schemaCacheDuration' => 3600, // 'schemaCacheDuration' => 3600,
), ),
), ),
); );
``` ```
Check [Database basics](database-basics.md) section in order to learn more on how to configure and use database Please read the [Database basics](database-basics.md) section to learn more on how to configure
connections. and use database connections.
Getting Data from Database Getting Data from Database
-------------------------- --------------------------
There are two ActiveRecord methods for getting data: There are two ActiveRecord methods for getting data from database:
- [[find()]] - [[find()]]
- [[findBySql()]] - [[findBySql()]]
They both return an [[ActiveQuery]] instance. Coupled with the various customization and query methods They both return an [[ActiveQuery]] instance. Coupled with various query methods provided
provided by [[ActiveQuery]], ActiveRecord supports very flexible and powerful data retrieval approaches. by [[ActiveQuery]], ActiveRecord supports very flexible and powerful data retrieval approaches.
The followings are some examples, The followings are some examples,
@ -83,13 +84,13 @@ $customers = Customer::find()
->all(); ->all();
// to return a single customer whose ID is 1: // to return a single customer whose ID is 1:
$customer = Customer::find(1);
// the above code is equivalent to the following:
$customer = Customer::find() $customer = Customer::find()
->where(array('id' => 1)) ->where(array('id' => 1))
->one(); ->one();
// or use the following shortcut approach:
$customer = Customer::find(1);
// to retrieve customers using a raw SQL statement: // to retrieve customers using a raw SQL statement:
$sql = 'SELECT * FROM tbl_customer'; $sql = 'SELECT * FROM tbl_customer';
$customers = Customer::findBySql($sql)->all(); $customers = Customer::findBySql($sql)->all();

Loading…
Cancel
Save