📜 ⬆️ ⬇️

Yii 1.1.7

Recently, the release of 1.1.7 PHP framework Yii was released, which included more than 90 bug fixes, improvements and new features.

A complete list of changes is available on the framework site , the most delicious of which we consider below.


')

REST style URL support



Now when using CUrlManager you can specify
method (GET, POST, PUT, etc.) in the verb property of the URL rule.
For example, the following rules GET request for post/123 will be processed
action post/view , and PUT or POST on post/123 - action post/update .

 return array( 'components'=>array( 'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( array('<controller>/view', 'pattern'=>'<controller:\w>/<id:\d+>', 'verb'=>'GET'), array('<controller>/update', 'pattern'=>'<controller:\w>/<id:\d+>', 'verb'=>'PUT, POST'), ), ), ), ); 


You can get the PUT and DELETE data using the methods CHttpRequest :: getPut () and CHttpRequest :: getDelete () .

Query caching



When caching a request, its result is stored in the cache and, in the case of a repeated request, it is taken from there directly.

Query caching is implemented for both DAO and AR. A few examples:

 //    $sql  1000      tbl_post $sql = 'SELECT * FROM tbl_post LIMIT 20'; $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post'); $rows = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll(); //  ,   AR $posts = Post::model()->cache(1000, $dependency)->findAll(); // query caching with relational AR $posts = Post::model()->cache(1000, $dependency)->with('author')->findAll(); 


Binding Parameters for Action Classes



In version 1.1.4, support has been added for automatic parameter filling for controller actions.
In this version, the opportunity has been extended to the actions described in the classes. Example:

 class UpdateAction extends CAction { public function run($id) { // $id     $_GET['id'] } } 


Transparent validation on the client



CActiveForm is a powerful widget that greatly facilitates the creation of a form and its validation.
Previously, it only supported server-side validation and AJAX validation.
In this version, client validation is added, which is faster and does not load the server.

To implement validation on the client there is no need for additional code. Validation
works on the basis of model rules in the same way as server-side validation.

Enable validation on the client by setting CActiveForm :: enableClientValidation
to true :

 <?php $form=$this->beginWidget('CActiveForm', array( 'enableClientValidation'=>true, )); ?> <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'password'); ?> <?php echo $form->passwordField($model,'password'); ?> <?php echo $form->error($model,'password'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Login'); ?> </div> <?php $this->endWidget(); ?> 


Passing Parameters to Relational Named Parameter Groups



Now you can pass parameters to relational named parameter groups. For example,
if there is a group rated in the Post model that accepts a minimum entry rating as a parameter,
you can use it from the User model as follows:

 $users=User::model()->findAll(array( 'with'=>array( 'posts'=>array( 'scopes'=>array( 'rated'=>5, ), ), ), )); 


Using 'through' with HAS_MANY and HAS_ONE



Support for the through option has been added to Active Record, which allows building relationships
similar MANY_MANY , but more flexible, allowing the acquisition and use of data from
middle table-bridge.

For example, you can get all the comments for all users of a particular group.

More information and examples can be found in the relevant section of the manual .

Using transactions in migrations



When performing a migration, there are situations when part of the migration is not applied.
and you want to roll back all that had time to apply. A good solution to this problem is to wrap the entire migration into a transaction.
Of course, you can do it directly, but using the new feature is more convenient.

Instead of implementing CDbMigration :: up () and CDbMigration :: down () ,
you can use CDbMigration :: safeUp () and CDbMigration :: safeDown () .
After that, the code will be wrapped in a transaction:

 class m101129_185401_create_news_table extends CDbMigration { public function safeUp() { $this->createTable('tbl_news', array( 'id' => 'pk', 'title' => 'string NOT NULL', 'content' => 'text', )); } public function safeDown() { $this->dropTable('tbl_news'); } } 


It is worth noting that not all DBMSs support transactions.

Register and use your script packages



CClientScript now allows you to register and use your script packages.
Previously, this feature was used exclusively for internal purposes.

A package can contain CSS, javascript, images and any other files that
must be shown to users. Package may depend on other packages. I.e
when registering a package, all packages on which it depends are recorded automatically.

Work with packages is as follows: first in
CClientScript :: packages packages are described, and then
they are registered with CClientScript :: registerPackage () .

Source: https://habr.com/ru/post/116416/


All Articles