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
4f70506c
Commit
4f70506c
authored
Jul 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minor doc fix.
parent
494fb8f6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
10 deletions
+10
-10
active-record.md
docs/guide/active-record.md
+10
-10
No files found.
docs/guide/active-record.md
View file @
4f70506c
...
@@ -133,10 +133,10 @@ $values = $customer->attributes;
...
@@ -133,10 +133,10 @@ $values = $customer->attributes;
```
```
Persist
ing Data to Database
Sav
ing Data to Database
-----------------------
----
-----------------------
ActiveRecord provides the following methods to insert, update and delete data:
ActiveRecord provides the following methods to insert, update and delete data
in the database
:
-
[
[save()
]
]
-
[
[save()
]
]
-
[
[insert()
]
]
-
[
[insert()
]
]
...
@@ -207,9 +207,9 @@ a one-many relationship. For example, a customer has many orders. And the [[hasO
...
@@ -207,9 +207,9 @@ a one-many relationship. For example, a customer has many orders. And the [[hasO
method declares a many-one or one-one relationship. For example, an order has one customer.
method declares a many-one or one-one relationship. For example, an order has one customer.
Both methods take two parameters:
Both methods take two parameters:
-
`$class`
: the name of the class
related models should use
. If specified without
-
`$class`
: the name of the class
of the related model(s)
. If specified without
a namespace, the namespace will be taken from the declaring class.
a namespace, the namespace
of the related model class
will be taken from the declaring class.
-
`$link`
: the association between columns from two tables. This should be given as an array.
-
`$link`
: the association between columns from t
he t
wo tables. This should be given as an array.
The keys of the array are the names of the columns from the table associated with
`$class`
,
The keys of the array are the names of the columns from the table associated with
`$class`
,
while the values of the array are the names of the columns from the declaring class.
while the values of the array are the names of the columns from the declaring class.
It is a good practice to define relationships based on table foreign keys.
It is a good practice to define relationships based on table foreign keys.
...
@@ -263,8 +263,8 @@ class Order extends \yii\db\ActiveRecord
...
@@ -263,8 +263,8 @@ class Order extends \yii\db\ActiveRecord
```
```
[
[ActiveRelation::via()
]
] method is similar to
[
[ActiveRelation::viaTable()
]
] except that
[
[ActiveRelation::via()
]
] method is similar to
[
[ActiveRelation::viaTable()
]
] except that
the first parameter of
[
[ActiveRelation::via()
]
] takes a relation name declared in the ActiveRecord class
.
the first parameter of
[
[ActiveRelation::via()
]
] takes a relation name declared in the ActiveRecord class
For example, the above
`items`
relation can be equivalently declared as follows:
instead of the pivot table name.
For example, the above
`items`
relation can be equivalently declared as follows:
```
php
```
php
class
Order
extends
\yii\db\ActiveRecord
class
Order
extends
\yii\db\ActiveRecord
...
@@ -315,7 +315,7 @@ How many SQL queries will be performed in the above code, assuming there are mor
...
@@ -315,7 +315,7 @@ How many SQL queries will be performed in the above code, assuming there are mor
the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query
the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query
is performed to bring back the customer's orders.
is performed to bring back the customer's orders.
To solve the above performance problem, you can use the so-called
*eager loading*
by calling
[
[ActiveQuery::with()
]
]:
To solve the above performance problem, you can use the so-called
*eager loading*
approach
by calling
[
[ActiveQuery::with()
]
]:
```
php
```
php
// SQL executed: SELECT * FROM tbl_customer LIMIT 100
// SQL executed: SELECT * FROM tbl_customer LIMIT 100
...
@@ -473,7 +473,7 @@ TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
...
@@ -473,7 +473,7 @@ TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
Imagine situation where you have to save something related to the main model in
[
[beforeSave()
]
],
Imagine situation where you have to save something related to the main model in
[
[beforeSave()
]
],
[
[afterSave()
]
],
[
[beforeDelete()
]
] and/or
[
[afterDelete()
]
] life cycle methods. Developer may come
[
[afterSave()
]
],
[
[beforeDelete()
]
] and/or
[
[afterDelete()
]
] life cycle methods. Developer may come
to solution of overriding ActiveRecord
[
[save()
]
] method with database transaction wrapping or
to
the
solution of overriding ActiveRecord
[
[save()
]
] method with database transaction wrapping or
even using transaction in controller action, which is strictly speaking doesn't seems to be a good
even using transaction in controller action, which is strictly speaking doesn't seems to be a good
practice (recall skinny-controller fat-model fundamental rule).
practice (recall skinny-controller fat-model fundamental rule).
...
...
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