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
9a1221e7
Commit
9a1221e7
authored
May 07, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wrapped code with github-style blocks with PHP highlighting
parent
9990dde1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
21 deletions
+32
-21
upgrade-from-v1.md
docs/guide/upgrade-from-v1.md
+32
-21
No files found.
docs/guide/upgrade-from-v1.md
View file @
9a1221e7
...
@@ -37,7 +37,7 @@ The `Object` class introduces a uniform way of configuring objects. Any descenda
...
@@ -37,7 +37,7 @@ The `Object` class introduces a uniform way of configuring objects. Any descenda
of
`Object`
should declare its constructor (if needed) in the following way so that
of
`Object`
should declare its constructor (if needed) in the following way so that
it can be properly configured:
it can be properly configured:
~~~
```
php
class
MyClass
extends
\yii\Object
class
MyClass
extends
\yii\Object
{
{
public
function
__construct
(
$param1
,
$param2
,
$config
=
array
())
public
function
__construct
(
$param1
,
$param2
,
$config
=
array
())
...
@@ -54,8 +54,8 @@ class MyClass extends \yii\Object
...
@@ -54,8 +54,8 @@ class MyClass extends \yii\Object
// ... initialization after configuration is applied
// ... initialization after configuration is applied
}
}
}
}
```
~~~
~~~
In the above, the last parameter of the constructor must take a configuration array
In the above, the last parameter of the constructor must take a configuration array
which contains name-value pairs for initializing the properties at the end of the constructor.
which contains name-value pairs for initializing the properties at the end of the constructor.
You can override the `init()` method to do initialization work that should be done after
You can override the `init()` method to do initialization work that should be done after
...
@@ -64,12 +64,13 @@ the configuration is applied.
...
@@ -64,12 +64,13 @@ the configuration is applied.
By following this convention, you will be able to create and configure a new object
By following this convention, you will be able to create and configure a new object
using a configuration array like the following:
using a configuration array like the following:
~~~
```php
$object = Yii::createObject(array(
$object = Yii::createObject(array(
'class' => 'MyClass',
'class' => 'MyClass',
'property1' => 'abc',
'property1' => 'abc',
'property2' => 'cde',
'property2' => 'cde',
), $param1, $param2);
), $param1, $param2);
```
~~~
~~~
...
@@ -80,28 +81,30 @@ There is no longer the need to define an `on`-method in order to define an event
...
@@ -80,28 +81,30 @@ There is no longer the need to define an `on`-method in order to define an event
Instead, you can use whatever event names. To attach a handler to an event, you should
Instead, you can use whatever event names. To attach a handler to an event, you should
use the
`on`
method now:
use the
`on`
method now:
~~~
```
php
$component
->
on
(
$eventName
,
$handler
);
$component
->
on
(
$eventName
,
$handler
);
// To detach the handler, use:
// To detach the handler, use:
// $component->off($eventName, $handler);
// $component->off($eventName, $handler);
```
~~~
~~~
When you attach a handler, you can now associate it with some parameters which can be later
When you attach a handler, you can now associate it with some parameters which can be later
accessed via the event parameter by the handler:
accessed via the event parameter by the handler:
~~~
```php
$component->on($eventName, $handler, $params);
$component->on($eventName, $handler, $params);
```
~~~
~~~
Because of this change, you can now use "global" events. Simply trigger and attach handlers to
Because of this change, you can now use "global" events. Simply trigger and attach handlers to
an event of the application instance:
an event of the application instance:
~~~
```
php
Yii
::
$app
->
on
(
$eventName
,
$handler
);
Yii
::
$app
->
on
(
$eventName
,
$handler
);
....
....
// this will trigger the event and cause $handler to be invoked.
// this will trigger the event and cause $handler to be invoked.
Yii
::
$app
->
trigger
(
$eventName
);
Yii
::
$app
->
trigger
(
$eventName
);
~~~
```
Path Alias
Path Alias
...
@@ -135,11 +138,12 @@ Because you can access the view object through the "view" application component,
...
@@ -135,11 +138,12 @@ Because you can access the view object through the "view" application component,
you can now render a view file like the following anywhere in your code, not necessarily
you can now render a view file like the following anywhere in your code, not necessarily
in controllers or widgets:
in controllers or widgets:
~~~
```
php
$content
=
Yii
::
$app
->
view
->
renderFile
(
$viewFile
,
$params
);
$content
=
Yii
::
$app
->
view
->
renderFile
(
$viewFile
,
$params
);
// You can also explicitly create a new View instance to do the rendering
// You can also explicitly create a new View instance to do the rendering
// $view = new View;
// $view = new View;
// $view->renderFile($viewFile, $params);
// $view->renderFile($viewFile, $params);
```
~~~
~~~
Also, there is no more `CClientScript` in Yii 2.0. The `View` class has taken over its role
Also, there is no more `CClientScript` in Yii 2.0. The `View` class has taken over its role
...
@@ -165,7 +169,7 @@ validation under which scenario. Child classes should overwrite `scenarios()` to
...
@@ -165,7 +169,7 @@ validation under which scenario. Child classes should overwrite `scenarios()` to
a list of scenarios and the corresponding attributes that need to be validated when
a list of scenarios and the corresponding attributes that need to be validated when
`validate()` is called. For example,
`validate()` is called. For example,
~~~
```php
public function scenarios()
public function scenarios()
{
{
return array(
return array(
...
@@ -173,6 +177,7 @@ public function scenarios()
...
@@ -173,6 +177,7 @@ public function scenarios()
'frontend' => array('email', '!name'),
'frontend' => array('email', '!name'),
);
);
}
}
```
~~~
~~~
This method also determines which attributes are safe and which are not. In particular,
This method also determines which attributes are safe and which are not. In particular,
...
@@ -194,13 +199,14 @@ sending them out. You have to `echo` them explicitly, e.g., `echo $this->render(
...
@@ -194,13 +199,14 @@ sending them out. You have to `echo` them explicitly, e.g., `echo $this->render(
A new method called
`populate()`
is introduced to simplify the data population from user inputs
A new method called
`populate()`
is introduced to simplify the data population from user inputs
to a model. For example,
to a model. For example,
~~~
```
php
$model
=
new
Post
;
$model
=
new
Post
;
if
(
$this
->
populate
(
$_POST
,
$model
))
{
...
}
if
(
$this
->
populate
(
$_POST
,
$model
))
{
...
}
// which is equivalent to:
// which is equivalent to:
if
(
isset
(
$_POST
[
'Post'
]))
{
if
(
isset
(
$_POST
[
'Post'
]))
{
$post
->
attributes
=
$_POST
[
'Post'
];
$post
->
attributes
=
$_POST
[
'Post'
];
}
}
```
~~~
~~~
...
@@ -256,7 +262,7 @@ define a new filter. To use a filter, you should attach the filter class to the
...
@@ -256,7 +262,7 @@ define a new filter. To use a filter, you should attach the filter class to the
as a behavior. For example, to use the `AccessControl` filter, you should have the following
as a behavior. For example, to use the `AccessControl` filter, you should have the following
code in a controller:
code in a controller:
~~~
```php
public function behaviors()
public function behaviors()
{
{
return array(
return array(
...
@@ -269,6 +275,7 @@ public function behaviors()
...
@@ -269,6 +275,7 @@ public function behaviors()
),
),
);
);
}
}
```
~~~
~~~
...
@@ -301,7 +308,7 @@ Yii 2.0 introduces the *field* concept for building a form using `ActiveForm`. A
...
@@ -301,7 +308,7 @@ Yii 2.0 introduces the *field* concept for building a form using `ActiveForm`. A
is a container consisting of a label, an input, and an error message. It is represented
is a container consisting of a label, an input, and an error message. It is represented
as an
`ActiveField`
object. Using fields, you can build a form more cleanly than before:
as an
`ActiveField`
object. Using fields, you can build a form more cleanly than before:
~~~
```
php
<?php
$form
=
$this
->
beginWidget
(
'yii\widgets\ActiveForm'
);
?>
<?php
$form
=
$this
->
beginWidget
(
'yii\widgets\ActiveForm'
);
?>
<?php
echo
$form
->
field
(
$model
,
'username'
)
->
textInput
();
?>
<?php
echo
$form
->
field
(
$model
,
'username'
)
->
textInput
();
?>
<?php
echo
$form
->
field
(
$model
,
'password'
)
->
passwordInput
();
?>
<?php
echo
$form
->
field
(
$model
,
'password'
)
->
passwordInput
();
?>
...
@@ -309,6 +316,7 @@ as an `ActiveField` object. Using fields, you can build a form more cleanly than
...
@@ -309,6 +316,7 @@ as an `ActiveField` object. Using fields, you can build a form more cleanly than
<?php
echo
Html
::
submitButton
(
'Login'
);
?>
<?php
echo
Html
::
submitButton
(
'Login'
);
?>
</div>
</div>
<?php
$this
->
endWidget
();
?>
<?php
$this
->
endWidget
();
?>
```
~~~
~~~
...
@@ -319,7 +327,7 @@ In 1.1, query building is scattered among several classes, including `CDbCommand
...
@@ -319,7 +327,7 @@ In 1.1, query building is scattered among several classes, including `CDbCommand
`CDbCriteria`, and `CDbCommandBuilder`. Yii 2.0 uses `Query` to represent a DB query
`CDbCriteria`, and `CDbCommandBuilder`. Yii 2.0 uses `Query` to represent a DB query
and `QueryBuilder` to generate SQL statements from query objects. For example,
and `QueryBuilder` to generate SQL statements from query objects. For example,
~~~
```php
$query = new \yii\db\Query;
$query = new \yii\db\Query;
$query->select('id, name')
$query->select('id, name')
->from('tbl_user')
->from('tbl_user')
...
@@ -328,6 +336,7 @@ $query->select('id, name')
...
@@ -328,6 +336,7 @@ $query->select('id, name')
$command = $query->createCommand();
$command = $query->createCommand();
$sql = $command->sql;
$sql = $command->sql;
$rows = $command->queryAll();
$rows = $command->queryAll();
```
~~~
~~~
Best of all, such query building methods can be used together with
`ActiveRecord`
,
Best of all, such query building methods can be used together with
`ActiveRecord`
,
...
@@ -342,7 +351,7 @@ is about relational ActiveRecord query. In 1.1, you have to declare the relation
...
@@ -342,7 +351,7 @@ is about relational ActiveRecord query. In 1.1, you have to declare the relation
in the
`relations()`
method. In 2.0, this is done via getter methods that return
in the
`relations()`
method. In 2.0, this is done via getter methods that return
an
`ActiveQuery`
object. For example, the following method declares an "orders" relation:
an
`ActiveQuery`
object. For example, the following method declares an "orders" relation:
~~~
```
php
class
Customer
extends
\yii\db\ActiveRecord
class
Customer
extends
\yii\db\ActiveRecord
{
{
public
function
getOrders
()
public
function
getOrders
()
...
@@ -350,6 +359,7 @@ class Customer extends \yii\db\ActiveRecord
...
@@ -350,6 +359,7 @@ class Customer extends \yii\db\ActiveRecord
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
));
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
));
}
}
}
}
```
~~~
~~~
You can use `$customer->orders` to access the customer's orders. You can also
You can use `$customer->orders` to access the customer's orders. You can also
...
@@ -366,7 +376,7 @@ by filtering with the primary keys of the primary records.
...
@@ -366,7 +376,7 @@ by filtering with the primary keys of the primary records.
Yii 2.0 no longer uses the `model()` method when performing queries. Instead, you
Yii 2.0 no longer uses the `model()` method when performing queries. Instead, you
use the `find()` method like the following:
use the `find()` method like the following:
~~~
```php
// to retrieve all *active* customers and order them by their ID:
// to retrieve all *active* customers and order them by their ID:
$customers = Customer::find()
$customers = Customer::find()
->where(array('status' => $active))
->where(array('status' => $active))
...
@@ -374,6 +384,7 @@ $customers = Customer::find()
...
@@ -374,6 +384,7 @@ $customers = Customer::find()
->all();
->all();
// return the customer whose PK is 1
// return the customer whose PK is 1
$customer = Customer::find(1);
$customer = Customer::find(1);
```
~~~
~~~
The
`find()`
method returns an instance of
`ActiveQuery`
which is a subclass of
`Query`
.
The
`find()`
method returns an instance of
`ActiveQuery`
which is a subclass of
`Query`
.
...
@@ -383,10 +394,9 @@ Instead of returning ActiveRecord objects, you may call `ActiveQuery::asArray()`
...
@@ -383,10 +394,9 @@ Instead of returning ActiveRecord objects, you may call `ActiveQuery::asArray()`
return results in terms of arrays. This is more efficient and is especially useful
return results in terms of arrays. This is more efficient and is especially useful
when you need to return large number of records. For example,
when you need to return large number of records. For example,
~~~
```
php
$customers
=
Customer
::
find
()
->
asArray
()
->
all
();
$customers
=
Customer
::
find
()
->
asArray
()
->
all
();
~~~
```
By default, ActiveRecord now only saves dirty attributes. In 1.1, all attributes
By default, ActiveRecord now only saves dirty attributes. In 1.1, all attributes
would be saved to database when you call
`save()`
, regardless they are changed or not,
would be saved to database when you call
`save()`
, regardless they are changed or not,
...
@@ -401,11 +411,11 @@ within double curly brackets is treated as a table name, and a name enclosed wit
...
@@ -401,11 +411,11 @@ within double curly brackets is treated as a table name, and a name enclosed wit
double square brackets is treated as a column name. They will be quoted according to
double square brackets is treated as a column name. They will be quoted according to
the database driver being used. For example,
the database driver being used. For example,
~~~
```
php
$command
=
$connection
->
createCommand
(
'SELECT [[id]] FROM {{posts}}'
);
$command
=
$connection
->
createCommand
(
'SELECT [[id]] FROM {{posts}}'
);
echo
$command
->
sql
;
// MySQL: SELECT `id` FROM `posts`
echo
$command
->
sql
;
// MySQL: SELECT `id` FROM `posts`
```
~~~
~~~
This feature is especially useful if you are developing an application that supports
This feature is especially useful if you are developing an application that supports
different DBMS.
different DBMS.
...
@@ -426,12 +436,13 @@ parameters. For example, if you have rule declared as follows, then it will matc
...
@@ -426,12 +436,13 @@ parameters. For example, if you have rule declared as follows, then it will matc
both `post/popular` and `post/1/popular`. In 1.1, you would have to use two rules to achieve
both `post/popular` and `post/1/popular`. In 1.1, you would have to use two rules to achieve
the same goal.
the same goal.
~~~
```php
array(
array(
'pattern' => 'post/<page:\d+>/<tag>',
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'route' => 'post/index',
'defaults' => array('page' => 1),
'defaults' => array('page' => 1),
)
)
```
~~~
~~~
...
...
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