📜 ⬆️ ⬇️

Phalcon 1.0.0 First Beta

Today, the Phalcon development team has released the first beta version of the Phalcon 1.0.0 framework. For those who do not know: Phalcon is a PHP framework written in C and working as an extension for PHP, you can read about it on Habré in Phalcon articles - compiled PHP MVC Framework and Phalcon: Let's learn from an example .
The purpose of this release is to get feedback from the community and find the maximum errors.



A brief announcement of the important innovations of this version:
  1. Layered caching
  2. Improvements in the Volt template engine
  3. Horizontal and vertical sharding
  4. Record Snapshots
  5. Dynamic update
  6. Validation



1. Multi-level caching


The new feature of the caching component allows the developer to implement the cache in several levels. The opportunity will be useful when saving the cache in several places (caching systems) with different lifetimes, and then reading them one by one starting with the fastest (in the order of registration) and ending with the slowest until all of them expire.
<?php $ultraFastFrontend = new Phalcon\Cache\Frontend\Data(array( "lifetime" => 3600 )); $fastFrontend = new Phalcon\Cache\Frontend\Data(array( "lifetime" => 86400 )); $slowFrontend = new Phalcon\Cache\Frontend\Data(array( "lifetime" => 604800 )); //Backends are registered from the fastest to the slower $cache = new \Phalcon\Cache\Multiple(array( new Phalcon\Cache\Backend\Apc($frontCache, array( "prefix" => 'cache', )), new Phalcon\Cache\Backend\Memcache($fastFrontCache, array( "prefix" => 'cache', "host" => "localhost", "port" => "11211" )), new Phalcon\Cache\Backend\File($slowestFrontCache, array( "prefix" => 'cache', "cacheDir" => "../app/cache/" )); )); // ,       $cache->save('my-key', $data); // ,          $data = $cache->get('my-key'); 

')

2. Improvements in the Volt template engine


Volt (also written in C) received several new features in this version:
 {#   #} {{ total > 0 ? total|format('%0.2f') : '0.0' }} {#  For-Else #} {% for robot in robots %} {{ robot.name }} {% else %} There are no robots {% endfor %} {#    #} <table> {% for robot in robots %} {% if loop.first %} <thead> <tr> <th>Position</th> <th>Id</th> <th>Name</th> </tr> </thead>ae <tbody> {% endif %} <tr> <th>{{ loop.index }}</th> <th>{{ robot.id }}</th> <th>{{ robot.name }}</th> </tr> {% if loop.last %} <tbody> {% endif %} {% endfor %} </table> {#     (Space control delimiters) #} <ul> {%- for robot in robots -%} <li> {{- robot.name -}}</li> {%- endfor %} </ul> 



3. Horizontal and vertical sharding


Now you can use different connections for writing and reading from the database. The utility manifests itself when working with the master / slave configuration of the DBMS:
 class Robots extends Phalcon\Mvc\Model { public function initialize() { $this->setReadConnectionService('dbSlave'); $this->setWriteConnectionService('dbMaster'); } } 


It is also possible horizontal sharding, when the connection is selected depending on the requested data:
 class Robots extends Phalcon\Mvc\Model { public function selectReadConnection($intermediate, $bindParams, $bindTypes) { // ,      'where' if (isset($intermediate['where'])) { $conditions = $intermediate['where']; //        if ($conditions['left']['name'] == 'id') { $id = $conditions['right']['value']; if ($id > 0 && $id < 10000) { return $this->getDI()->get('dbShard1'); } if ($id > 10000) { return $this->getDI()->get('dbShard2'); } } } //        return $this->getDI()->get('dbShard0'); } } 



4. Record Snapshots


Now the models can be set property to store the field values ​​at the time of the request. You can use this function to audit or verify changes in field values ​​during work:
 class Robots extends Phalcon\Mvc\Model { public function initalize() { $this->keepSnapshots(true); } } 


So you can check the values ​​of which fields have been changed:
 $robot = new Robots(); $robot->name = 'Other name'; var_dump($robot->getChangedFields()); // ['name'] var_dump($robot->hasChanged('name')); // true var_dump($robot->hasChanged('type')); // false 



5. Dynamic update


Allows the ORM when forming the SQL UPDATE query to use in it only those fields whose values ​​have been changed, and not to collect all the fields and all their values. This reduces traffic between the application servers and the database, which in turn can have a positive effect on performance:
 class Robots extends Phalcon\Mvc\Model { public function initalize() { $this->useDynamicUpdate(true); } } 



6. Validation


The new component Phalcon \ Validation allows for independent data verification. The component is based on checks already implemented in ORM and ODM. With it, you can check the data is not associated with any model or collection:

 $validation = new Phalcon\Validation(); $validation ->add('name', new PresenceOf(array( 'message' => ' name   ' ))) ->add('name', new StringLength(array( 'min' => 5, 'minimumMessage' => '  name  ' ))) ->add('email', new PresenceOf(array( 'message' => ' email    ' ))) ->add('email', new Email(array( 'message' => 'Email   ' ))) ->add('login', new PresenceOf(array( 'message' => ' login   ' ))); $messages = $validation->validate($_POST); if (count($messages)) { foreach ($messages as $message) { echo $message; } } 


This release also contains other improvements and fixes, you can find out all of them after reading the full list of changes or in the Russian in the Russian support group Phalcon VKontakte.

Help with testing


You can get this version from GitHub from branch 1.0.0:

 git clone http://github.com/phalcon/cphalcon cd build git checkout 1.0.0 sudo ./install 


Windows users can download a ready-made DLL on the download page.

We are happy to accept your comments and suggestions on Phosphorum , Stack Overflow or Google Group . If you find an error and can form a test to repeat it or solve it, send us a pull request or create an issue on GitHub .

PS Translation free with additions, the original is here .
To support the framework, a vkontakte group has been created, a Russian website and documentation localization has begun, join us!
There is also another translation of the Phalcon PHP FrameWork documentation and examples .

Rights to the original photo belong to Simon Roy: 500px.com/photo/7924712

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


All Articles