|
|
|
@ -13,7 +13,7 @@ The most obvious change in Yii 2.0 is the use of namespaces. Almost every core c
|
|
|
|
|
is namespaced, e.g., `yii\web\Request`. The "C" prefix is no longer used in class names. |
|
|
|
|
The naming of the namespaces follows the directory structure. For example, `yii\web\Request` |
|
|
|
|
indicates the corresponding class file is `web/Request.php` under the Yii framework folder. |
|
|
|
|
You can use any core class without explicitly include that class file, thanks to the Yii |
|
|
|
|
You can use any core class without explicitly including that class file, thanks to the Yii |
|
|
|
|
class loader. |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -117,17 +117,17 @@ supported in most places in the Yii core code. For example, `FileCache::cachePat
|
|
|
|
|
both a path alias and a normal directory path. |
|
|
|
|
|
|
|
|
|
Path alias is also closely related with class namespaces. It is recommended that a path |
|
|
|
|
alias defined for each root namespace so that you can use Yii class autoloader without |
|
|
|
|
alias be defined for each root namespace so that you can use Yii the class autoloader without |
|
|
|
|
any further configuration. For example, because `@yii` refers to the Yii installation directory, |
|
|
|
|
a class like `yii\web\Request` can be autoloaded by Yii. If you use a third party library |
|
|
|
|
such as Zend Framework, you may define a path alias `@Zend` which refers to its installation directory. |
|
|
|
|
And Yii will be able to autoload any class in this library. |
|
|
|
|
such as Zend Framework, you may define a path alias `@Zend` which refers to its installation |
|
|
|
|
directory and Yii will be able to autoload any class in this library. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
View |
|
|
|
|
---- |
|
|
|
|
|
|
|
|
|
Yii 2.0 introduces a `View` class to represent the view part in the MVC pattern. |
|
|
|
|
Yii 2.0 introduces a `View` class to represent the view part of the MVC pattern. |
|
|
|
|
It can be configured globally through the "view" application component. It is also |
|
|
|
|
accessible in any view file via `$this`. This is one of the biggest changes compared to 1.1: |
|
|
|
|
**`$this` in a view file no longer refers to the controller or widget object.** |
|
|
|
@ -159,7 +159,7 @@ extension for your Smarty views, or `twig` for Twig views. You may also configur
|
|
|
|
|
Models |
|
|
|
|
------ |
|
|
|
|
|
|
|
|
|
A model is now associated with a form name returned its `formName()` method. This is |
|
|
|
|
A model is now associated with a form name returned by its `formName()` method. This is |
|
|
|
|
mainly used when using HTML forms to collect user inputs for a model. Previously in 1.1, |
|
|
|
|
this is usually hardcoded as the class name of the model. |
|
|
|
|
|
|
|
|
@ -235,7 +235,7 @@ Previously in 1.1, you would have to enter the widget class names as strings via
|
|
|
|
|
Themes |
|
|
|
|
------ |
|
|
|
|
|
|
|
|
|
Theme works completely different in 2.0. It is now based on a path map to "translate" a source |
|
|
|
|
Themes work completely different in 2.0. They are now based on a path map to "translate" a source |
|
|
|
|
view into a themed view. For example, if the path map for a theme is |
|
|
|
|
`array('/www/views' => '/www/themes/basic')`, then the themed version for a view file |
|
|
|
|
`/www/views/site/index.php` will be `/www/themes/basic/site/index.php`. |
|
|
|
@ -250,7 +250,7 @@ application component.
|
|
|
|
|
Console Applications |
|
|
|
|
-------------------- |
|
|
|
|
|
|
|
|
|
Console applications are now composed by controllers, too, like Web applications. In fact, |
|
|
|
|
Console applications are now composed by controllers, like Web applications. In fact, |
|
|
|
|
console controllers and Web controllers share the same base controller class. |
|
|
|
|
|
|
|
|
|
Each console controller is like `CConsoleCommand` in 1.1. It consists of one or several |
|
|
|
@ -300,7 +300,7 @@ public function behaviors()
|
|
|
|
|
Assets |
|
|
|
|
------ |
|
|
|
|
|
|
|
|
|
Yii 2.0 introduces a new concept called *asset bundle*. It is a bit similar to script |
|
|
|
|
Yii 2.0 introduces a new concept called *asset bundle*. It is similar to script |
|
|
|
|
packages (managed by `CClientScript`) in 1.1, but with better support. |
|
|
|
|
|
|
|
|
|
An asset bundle is a collection of asset files (e.g. JavaScript files, CSS files, image files, etc.) |
|
|
|
@ -315,7 +315,7 @@ Static Helpers
|
|
|
|
|
|
|
|
|
|
Yii 2.0 introduces many commonly used static helper classes, such as `Html`, `ArrayHelper`, |
|
|
|
|
`StringHelper`. These classes are designed to be easily extended. Note that static classes |
|
|
|
|
are usually hard to be extended because of the fixed class name references. But Yii 2.0 |
|
|
|
|
are usually hard to extend because of the fixed class name references. But Yii 2.0 |
|
|
|
|
introduces the class map (via `Yii::$classMap`) to overcome this difficulty. |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -343,7 +343,7 @@ Query Builder
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
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; |
|
|
|
@ -365,7 +365,7 @@ ActiveRecord
|
|
|
|
|
------------ |
|
|
|
|
|
|
|
|
|
ActiveRecord has undergone significant changes in Yii 2.0. The most important one |
|
|
|
|
is about relational ActiveRecord query. In 1.1, you have to declare the relations |
|
|
|
|
is the relational ActiveRecord query. In 1.1, you have to declare the relations |
|
|
|
|
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: |
|
|
|
|
|
|
|
|
@ -392,7 +392,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 |
|
|
|
|
use the `find()` method like the following: |
|
|
|
|
use the `find()` method: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
// to retrieve all *active* customers and order them by their ID: |
|
|
|
@ -410,14 +410,14 @@ Therefore, you can use all query methods of `Query`.
|
|
|
|
|
|
|
|
|
|
Instead of returning ActiveRecord objects, you may call `ActiveQuery::asArray()` to |
|
|
|
|
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 a large number of records: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
$customers = Customer::find()->asArray()->all(); |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
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, |
|
|
|
|
are saved to database when you call `save()`, regardless of having changed or not, |
|
|
|
|
unless you explicitly list the attributes to save. |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -427,7 +427,7 @@ Auto-quoting Table and Column Names
|
|
|
|
|
Yii 2.0 supports automatic quoting of database table and column names. A name enclosed |
|
|
|
|
within double curly brackets is treated as a table name, and a name enclosed within |
|
|
|
|
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: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
$command = $connection->createCommand('SELECT [[id]] FROM {{posts}}'); |
|
|
|
|