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
0544d775
Commit
0544d775
authored
Sep 26, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #5175. [skip ci]
parent
6bef0e62
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
4 deletions
+4
-4
structure-models.md
docs/guide/structure-models.md
+4
-4
No files found.
docs/guide/structure-models.md
View file @
0544d775
...
@@ -301,7 +301,7 @@ is associated with one or multiple active rules declared in `rules()`.
...
@@ -301,7 +301,7 @@ is associated with one or multiple active rules declared in `rules()`.
## Massive Assignment <a name="massive-assignment"></a>
## Massive Assignment <a name="massive-assignment"></a>
Massive assignment is a convenient way of populating a model with user inputs using a single line of code.
Massive assignment is a convenient way of populating a model with user inputs using a single line of code.
It populates the attributes of a model by assigning the input data directly to the
[
[yii\base\Model::attributes
]
]
It populates the attributes of a model by assigning the input data directly to the
[
[yii\base\Model::
$
attributes
]
]
property. The following two pieces of code are equivalent, both trying to assign the form data submitted by end users
property. The following two pieces of code are equivalent, both trying to assign the form data submitted by end users
to the attributes of the
`ContactForm`
model. Clearly, the former, which uses massive assignment, is much cleaner
to the attributes of the
`ContactForm`
model. Clearly, the former, which uses massive assignment, is much cleaner
and less error prone than the latter:
and less error prone than the latter:
...
@@ -395,7 +395,7 @@ In the first step, models are converted into arrays; in the second step, the arr
...
@@ -395,7 +395,7 @@ In the first step, models are converted into arrays; in the second step, the arr
target formats. You may just focus on the first step, because the second step can be achieved by generic
target formats. You may just focus on the first step, because the second step can be achieved by generic
data formatters, such as
[
[yii\web\JsonResponseFormatter
]
].
data formatters, such as
[
[yii\web\JsonResponseFormatter
]
].
The simplest way of converting a model into an array is to use the
[
[yii\base\Model::attributes
]
] property.
The simplest way of converting a model into an array is to use the
[
[yii\base\Model::
$
attributes
]
] property.
For example,
For example,
```
php
```
php
...
@@ -403,11 +403,11 @@ $post = \app\models\Post::findOne(100);
...
@@ -403,11 +403,11 @@ $post = \app\models\Post::findOne(100);
$array
=
$post
->
attributes
;
$array
=
$post
->
attributes
;
```
```
By default, the
[
[yii\base\Model::attributes
]
] property will return the values of
*all*
attributes
By default, the
[
[yii\base\Model::
$
attributes
]
] property will return the values of
*all*
attributes
declared in
[
[yii\base\Model::attributes()
]
].
declared in
[
[yii\base\Model::attributes()
]
].
A more flexible and powerful way of converting a model into an array is to use the
[
[yii\base\Model::toArray()
]
]
A more flexible and powerful way of converting a model into an array is to use the
[
[yii\base\Model::toArray()
]
]
method. Its default behavior is the same as that of
[
[yii\base\Model::attributes
]
]. However, it allows you
method. Its default behavior is the same as that of
[
[yii\base\Model::
$
attributes
]
]. However, it allows you
to choose which data items, called
*fields*
, to be put in the resulting array and how they should be formatted.
to choose which data items, called
*fields*
, to be put in the resulting array and how they should be formatted.
In fact, it is the default way of exporting models in RESTful Web service development, as described in
In fact, it is the default way of exporting models in RESTful Web service development, as described in
the
[
Response Formatting
](
rest-response-formatting.md
)
.
the
[
Response Formatting
](
rest-response-formatting.md
)
.
...
...
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