📜 ⬆️ ⬇️

Phalcon 2.0.4 release

As part of the schedule of our three-weekly minor releases, we are pleased to announce that Phalcon 2.0.4 has been released!

The number of improvements and fixes has increased significantly compared with other releases of 2.0.x:

Changes



Highlights


Typed placeholders in ORM


Prior to this version, only standard placeholders (strings and numbers ) were supported in PHQL . They allowed to bind parameters to avoid SQL injections:

 $phql = "SELECT * FROM Store\Robots WHERE id > :id:"; $robots = $this->modelsManager->executeQuery($phql, ['id' => 100]); 

However, some DBMS require additional actions when using placeholders, such as specifying the type:
')
 use Phalcon\Db\Column; // ... $phql = "SELECT * FROM Store\Robots LIMIT :number:"; $robots = $this->modelsManager->executeQuery( $phql, ['number' => 10], Column::BIND_PARAM_INT ); 

To facilitate this task, Phalcon 2.0.4 introduces typed placeholders that work exactly as before, but with the ability to specify the type:

 $phql = "SELECT * FROM Store\Robots LIMIT {number:int}"; $robots = $this->modelsManager->executeQuery( $phql, ['number' => 10] ); $phql = "SELECT * FROM Store\Robots WHERE name <> {name:str}"; $robots = $this->modelsManager->executeQuery( $phql, ['name' => $name] ); 

You can also omit the type indication if you do not need:

 $phql = "SELECT * FROM Store\Robots WHERE name <> {name}"; $robots = $this->modelsManager->executeQuery( $phql, ['name' => $name] ); 

Typed placeholders are also more functional, since now we can bind a static array without having to pass each element separately as a placeholder:

 $phql = "SELECT * FROM Store\Robots WHERE id IN ({ids:array})"; $robots = $this->modelsManager->executeQuery( $phql, ['ids' => [1, 2, 3, 4]] ); 

The following types are available:
Type ofType constantExample
strColumn::BIND_PARAM_STR{name:str}
intColumn::BIND_PARAM_INT{number:int}
doubleColumn::BIND_PARAM_DECIMAL{price:double}
boolColumn::BIND_PARAM_BOOL{enabled:bool}
blobColumn::BIND_PARAM_BLOB{image:blob}
nullColumn::BIND_PARAM_NULL{exists:null}
arrayArray of Column::BIND_PARAM_STR{codes:array}
array-strArray of Column::BIND_PARAM_STR{names:array}
array-intArray of Column::BIND_PARAM_INT{flags:array}

Validation of bound parameters

By default, parameters associated with placeholders did not support type indications, but now it is possible to check the types of parameters before starting work with PDO.

The classic situation when a problem arises is to transfer the string to the LIMIT / OFFSET placeholder:

 $number = '100'; $robots = $modelsManager->executeQuery( 'SELECT * FROM Some\Robots LIMIT {number:int}', ['number' => $number] ); 

This code will throw the following exception:

 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''100'' at line 1' in /Users/scott/demo.php:78 

This is because 100 is a string variable. Easy to fix, you just need to cast the type to int:

 $number = '100'; $robots = $modelsManager->executeQuery( 'SELECT * FROM Some\Robots LIMIT {number:int}', ['number' => (int) $number] ); 

However, this solution requires the developer to pay special attention to working with types. To make the task easier and avoid unexpected exceptions, you can get Phalcon to do this work for you:

 \Phalcon\Db::setup(['forceCasting' => true]); 

The following actions are performed according to the binding of the specified type:

Type ofAct
Column::BIND_PARAM_STRSends the value as a native type PHP string
Column::BIND_PARAM_INTPass value as native php integer type
Column::BIND_PARAM_BOOLPass value as native php type boolean
Column::BIND_PARAM_DECIMALSends value as native PHP type double

Type casting from PDO values

The values ​​returned from the system database are always transferred as string values ​​by the PDO, regardless of whether the value belongs to a numeric or logical data type of a column. This is because some column types cannot be represented using native PHP types due to their size limit.

For example, a value of type BIGINT in MySQL can store large integers that cannot be represented as a 32-bit integer in PHP. Because of this, PDO and ORM defaults to a secure solution and leave all values ​​as strings.

However, some developers may find this unexpected and inconvenient. With Phalcon 2.0.4, you can configure ORM to automatically cast types to the appropriate PHP primitives, provided that it is safe:

 \Phalcon\Mvc\Model::setup(['castOnHydrate' => true]); 

Thus, you can use strict comparison operators or make assumptions about the type of variables:

 $robot = Robots::findFirst(); if ($robot->id === 11) { echo $robot->name; } 

Links to conditional operators

With 2.0.4, you can create relationships based on conditional statements. Phalcon will take care of the rest :)

 // Companies have invoices issued to them (paid/unpaid) // Invoices model class Invoices extends Phalcon\Mvc\Model { public function getSource() { return 'invoices'; } } // Companies model class Companies extends Phalcon\Mvc\Model { public function getSource() { return 'companies'; } public function initialize() { // All invoices relationship $this->hasMany( 'id', 'Invoices', 'inv_id', [ 'alias' => 'invoices', 'reusable' => true, ] ); // Paid invoices relationship $this->hasMany( 'id', 'Invoices', 'inv_id', [ 'alias' => 'invoicesPaid', 'reusable' => true, 'params' => [ 'conditions' => "inv_status = 'paid'" ] ] ); // Unpaid invoices relationship $this->hasMany( 'id', 'Invoices', 'inv_id', [ 'alias' => 'invoicesUnpaid', 'reusable' => true, 'params' => [ 'conditions' => "inv_status <> 'paid'" ] ] ); } } 

Update / Install


This version can be installed from the master branch. If you have not installed Zephir, run the following commands:

 git clone http://github.com/phalcon/cphalcon git checkout master cd ext sudo ./install 

If you already have Zephir installed:

 git clone http://github.com/phalcon/cphalcon git checkout master zephir fullclean zephir build 

Please note that when you run the installation script will replace the already installed version of Phalcon.

Windows DLLs are available on the download page .

See the upgrade guide if you want to upgrade to Phalcon 2.0.x from 1.3.x.

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


All Articles