Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
494fb8f6
Commit
494fb8f6
authored
Jul 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minor doc improvement.
parent
2fecd5eb
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
15 deletions
+16
-15
active-record.md
docs/guide/active-record.md
+16
-15
No files found.
docs/guide/active-record.md
View file @
494fb8f6
...
@@ -3,7 +3,7 @@ Active Record
...
@@ -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 colum
n
s 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
...
@@ -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
...
@@ -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(
...
@@ -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()
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment