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
5670bc3d
Commit
5670bc3d
authored
Jun 06, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide WIP [skip ci]
parent
4d2cf383
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
32 deletions
+44
-32
structure-models.md
docs/guide/structure-models.md
+44
-32
No files found.
docs/guide/structure-models.md
View file @
5670bc3d
...
@@ -23,30 +23,15 @@ Please refer to the relevant documentation for more details about these advanced
...
@@ -23,30 +23,15 @@ Please refer to the relevant documentation for more details about these advanced
## Attributes <a name="attributes"></a>
## Attributes <a name="attributes"></a>
Attributes are the properties that represent business data. By default, attributes are
*non-static public*
Models represent business data in terms of
*attributes*
. Each attribute is like a publicly accessible property
member variables if your model class extends directly from
[
[yii\base\Model
]
]
.
of a model. The method
[
[yii\base\Model::attributes()
]
] specifies what attributes a model class has
.
The following code creates a
`ContactForm`
model class with four attributes:
`name`
,
`email`
,
`subject`
and
`body`
.
You can access an attribute like accessing a normal object property:
This model represents the input data that is received from an HTML form.
```
php
namespace
app\models
;
use
yii\base\Model
;
class
ContactForm
extends
Model
{
public
$name
;
public
$email
;
public
$subject
;
public
$body
;
}
```
Naturally, you can access an attribute like accessing a normal object property:
```
php
```
php
$model
=
new
\app\models\ContactForm
;
$model
=
new
\app\models\ContactForm
;
// "name" is an attribute of ContactForm
$model
->
name
=
'example'
;
$model
->
name
=
'example'
;
echo
$model
->
name
;
echo
$model
->
name
;
```
```
...
@@ -68,12 +53,33 @@ foreach ($model as $name => $value) {
...
@@ -68,12 +53,33 @@ foreach ($model as $name => $value) {
}
}
```
```
### Extending model attributes
The method
[
[yii\base\Model::attributes()
]
] defines and returns the names of the attributes in a model.
### Defining Attributes <a name="defining-attributes"></a>
You may override this method to support different ways of defining attributes. For example,
[
[yii\db\ActiveRecord
]
]
does so by returning table column names as attribute names. Note that you may also need to override the magic
By default, if your model class extends directly from
[
[yii\base\Model
]
], all its
*non-static public*
member
methods such as
`__get()`
,
`__set()`
so that the attributes can be accessed like normal object properties.
variables are attributes. For example, the
`ContactForm`
model class below has four attributes:
`name`
,
`email`
,
`subject`
and
`body`
. The
`ContactForm`
model is used to represent the input data received from an HTML form.
```
php
namespace
app\models
;
use
yii\base\Model
;
class
ContactForm
extends
Model
{
public
$name
;
public
$email
;
public
$subject
;
public
$body
;
}
```
You may override
[
[yii\base\Model::attributes()
]
] to define attributes in a different way. The method should
return the names of the attributes in a model. For example,
[
[yii\db\ActiveRecord
]
] does so by returning
the column names of the associated database table as its attribute names. Note that you may also need to
override the magic methods such as
`__get()`
,
`__set()`
so that the attributes can be accessed like
normal object properties.
## Attribute Labels <a name="attribute-labels"></a>
## Attribute Labels <a name="attribute-labels"></a>
...
@@ -82,6 +88,15 @@ When displaying values or getting input for attributes, you often need to displa
...
@@ -82,6 +88,15 @@ When displaying values or getting input for attributes, you often need to displa
with attributes. For example, given an attribute named
`firstName`
, you may want to display a label
`First Name`
with attributes. For example, given an attribute named
`firstName`
, you may want to display a label
`First Name`
which is more user-friendly when displayed to end users in places such as form inputs and error messages.
which is more user-friendly when displayed to end users in places such as form inputs and error messages.
You can get the label of an attribute by calling
[
[yii\base\Model::getAttributeLabel()
]
]. For example,
```
php
$model
=
new
\app\models\ContactForm
;
// displays "Label"
echo
$model
->
getAttributeLabel
(
'name'
);
```
By default, attribute labels are automatically generated from attribute names. The generation is done by
By default, attribute labels are automatically generated from attribute names. The generation is done by
the method
[
[yii\base\Model::generateAttributeLabel()
]
]. It will turn camel-case variable names into
the method
[
[yii\base\Model::generateAttributeLabel()
]
]. It will turn camel-case variable names into
multiple words with the first letter in each word in upper case. For example,
`username`
becomes
`Username`
,
multiple words with the first letter in each word in upper case. For example,
`username`
becomes
`Username`
,
...
@@ -292,13 +307,10 @@ According to the above validation steps, an attribute will be validated if and o
...
@@ -292,13 +307,10 @@ According to the above validation steps, an attribute will be validated if and o
an active attribute declared in
`scenarios()`
and it is associated with one or multiple active rules
an active attribute declared in
`scenarios()`
and it is associated with one or multiple active rules
declared in
`rules()`
.
declared in
`rules()`
.
Yii provides a set of built-in validators to support commonly needed data validation tasks. You may also
Yii provides a set of
[
built-in validators
](
tutorial-core-validators.md
)
to support commonly needed data
create your own validators by extending
[
[yii\validators\Validator
]
] or writing an inline validation method
validation tasks. You may also create your own validators by extending
[
[yii\validators\Validator
]
] or
within model classes. For more details about the built-in validators and how to create your own validators,
writing an inline validation method within model classes. For more details about the built-in validators
please refer to the
[
Input Validation
](
input-validation.md
)
section.
and how to create your own validators, please refer to the
[
Input Validation
](
input-validation.md
)
section.
> Note: As a rule of thumb, never trust the data coming from end users and always validate them before
putting them to some good use.
## Massive Assignment <a name="massive-assignment"></a>
## Massive Assignment <a name="massive-assignment"></a>
...
...
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