Another implementation is to use getters and setter functions, which control which properties are publicly available for that model. The benefit of using getters and setters is that the developer can perform transformations and validation checks on the values set for the model, which is impossible when using public properties. Additionally getters and setters allow for future changes without changing the interface of the model class. So if a field name changes, the only change needed will be in the private property of the model referenced in the relevant getter/setter and nowhere else in the code.

<?phpnamespaceStore\Toys;useInvalidArgumentException;usePhalcon\Mvc\Model;classRobotsextendsModel{protected$id;protected$name;protected$price;publicfunctiongetId(){return$this->id;}publicfunctionsetName($name){// The name is too short?if(strlen($name)<10){thrownewInvalidArgumentException('The name is too short');}$this->name=$name;}publicfunctiongetName(){return$this->name;}publicfunctionsetPrice($price){// Negative prices aren't allowedif($price<0){thrownewInvalidArgumentException("Price can't be negative");}$this->price=$price;}publicfunctiongetPrice(){// Convert the value to double before be usedreturn(double)$this->price;}}

Public properties provide less complexity in development. However getters/setters can heavily increase the testability, extensibility and maintainability of applications. Developers can decide which strategy is more appropriate for the application they are creating, depending on the needs of the application. The ORM is compatible with both schemes of defining properties.

Underscores in property names can be problematic when using getters and setters.

If you use underscores in your property names, you must still use camel case in your getter/setter declarations for use with magic methods. (e.g. $model->getPropertyName instead of $model->getProperty_name, $model->findByPropertyName instead of $model->findByProperty_name, etc.). As much of the system expects camel case, and underscores are commonly removed, it is recommended to name your properties in the manner shown throughout the documentation. You can use a column map (as described above) to ensure proper mapping of your properties to their database counterparts.

Понимание записей в объектах

Every instance of a model represents a row in the table. You can easily access record data by reading object properties. For example, for a table ‘robots’ with the records:

Эти объекты являются более мощными, чем стандартные массивы. One of the greatest features of the Phalcon\Mvc\Model\Resultset is that at any time there is only one record in memory. Это очень помогает в управлении памятью, особенно при работе с большими объемами данных.

Обратите внимание, что наборы результатов могут быть сериализованы и храниться в кэше бэкэнда. Phalcon\Cache can help with that task. However, serializing data causes Phalcon\Mvc\Model to retrieve all the data from the database in an array, thus consuming more memory while this process takes place.

Custom Resultsets

There are times that the application logic requires additional manipulation of the data as it is retrieved from the database. Previously, we would just extend the model and encapsulate the functionality in a class in the model or a trait, returning back to the caller usually an array of transformed data.

With custom resultsets, you no longer need to do that. The custom resultset will encapsulate the functionality that otherwise would be in the model and can be reused by other models, thus keeping the code DRY. This way, the find() method will no longer return the default Phalcon\Mvc\Model\Resultset, but instead the custom one. Phalcon allows you to do this by using the getResultsetClass() in your model.

<?php/**
* Find the robots
*/$robots=Robots::find(['conditions'=>'date between "2017-01-01" AND "2017-12-31"','order'=>'date']);/**
* Pass the data to the view
*/$this->view->mydata=$robots->getSomeData();

В метод save может быть передан массив, чтобы избежать назначения каждого столбца вручную. Phalcon\Mvc\Model will check if there are setters implemented for the columns passed in the array giving priority to them instead of assign directly the values of the attributes:

Without precautions mass assignment could allow attackers to set any database column's value. Only use this feature if you want to permit a user to insert/update every column in the model, even if those fields are not in the submitted form.

Режимы гидрации

As mentioned previously, resultsets are collections of complete objects, this means that every returned result is an object representing a row in the database. These objects can be modified and saved again to persistence:

Sometimes records are obtained only to be presented to a user in read-only mode, in these cases it may be useful to change the way in which records are represented to facilitate their handling. The strategy used to represent objects returned in a resultset is called ‘hydration mode’:

<?phpusePhalcon\Mvc\Model\Resultset;useStore\Toys\Robots;$robots=Robots::find();// Return every robot as an array$robots->setHydrateMode(Resultset::HYDRATE_ARRAYS);foreach($robotsas$robot){echo$robot['year'],PHP_EOL;}// Return every robot as a stdClass$robots->setHydrateMode(Resultset::HYDRATE_OBJECTS);foreach($robotsas$robot){echo$robot->year,PHP_EOL;}// Return every robot as a Robots instance$robots->setHydrateMode(Resultset::HYDRATE_RECORDS);foreach($robotsas$robot){echo$robot->year,PHP_EOL;}

Автоматически генерируемый столбец

Some models may have identity columns. These columns usually are the primary key of the mapped table. Phalcon\Mvc\Model can recognize the identity column omitting it in the generated SQL INSERT, so the database system can generate an auto-generated value for it. Always after creating a record, the identity field will be registered with the value generated in the database system for it:

<?php$robot->save();echo'The generated id is: ',$robot->id;

Phalcon\Mvc\Model is able to recognize the identity column. Depending on the database system, those columns may be serial columns like in PostgreSQL or auto_increment columns in the case of MySQL.

PostgreSQL uses sequences to generate auto-numeric values, by default, Phalcon tries to obtain the generated value from the sequence table_field_seq, for example: robots_id_seq, if that sequence has a different name, the getSequenceName() method needs to be implemented:

Пропуск столбцов

To tell Phalcon\Mvc\Model that always omits some fields in the creation and/or update of records in order to delegate the database system the assignation of the values by a trigger or a default:

<?phpnamespaceStore\Toys;usePhalcon\Mvc\Model;classRobotsextendsModel{publicfunctioninitialize(){// Skips fields/columns on both INSERT/UPDATE operations$this->skipAttributes(['year','price',]);// Skips only when inserting$this->skipAttributesOnCreate(['created_at',]);// Skips only when updating$this->skipAttributesOnUpdate(['modified_in',]);}}

This will ignore globally these fields on each INSERT/UPDATE operation on the whole application. If you want to ignore different attributes on different INSERT/UPDATE operations, you can specify the second parameter (boolean) - true for replacement. Forcing a default value can be done as follows:

Never use a Phalcon\Db\RawValue to assign external data (such as user input) or variable data. The value of these fields is ignored when binding parameters to the query. So it could be used to attack the application injecting SQL.

Динамические обновления

SQL UPDATE statements are by default created with every column defined in the model (full all-field SQL update). You can change specific models to make dynamic updates, in this case, just the fields that had changed are used to create the final SQL statement.

In some cases this could improve the performance by reducing the traffic between the application and the database server, this specially helps when the table has blob/text fields:

Независимое сопоставление столбцов

The ORM supports an independent column map, which allows the developer to use different column names in the model to the ones in the table. Phalcon will recognize the new column names and will rename them accordingly to match the respective columns in the database. This is a great feature when one needs to rename fields in the database without having to worry about all the queries in the code. A change in the column map in the model will take care of the rest. For example:

<?phpnamespaceStore\Toys;usePhalcon\Mvc\Model;classRobotsextendsModel{public$code;public$theName;public$theType;public$theYear;publicfunctioncolumnMap(){// Keys are the real names in the table and// the values their names in the applicationreturn['id'=>'code','the_name'=>'theName','the_type'=>'theType','the_year'=>'theYear',];}}

Then you can use the new names naturally in your code:

<?phpuseStore\Toys\Robots;// Find a robot by its name$robot=Robots::findFirst("theName = 'Voltron'");echo$robot->theName,"\n";// Get robots ordered by type$robot=Robots::find(['order'=>'theType DESC',]);foreach($robotsas$robot){echo'Code: ',$robot->code,"\n";}// Create a robot$robot=newRobots();$robot->code='10101';$robot->theName='Bender';$robot->theType='Industrial';$robot->theYear=2999;$robot->save();

Consider the following when renaming your columns:

References to attributes in relationships/validators must use the new names

Refer the real column names will result in an exception by the ORM

The independent column map allows you to:

Write applications using your own conventions

Eliminate vendor prefixes/suffixes in your code

Change column names without change your application code

Запись снимков

Specific models could be set to maintain a record snapshot when they’re queried. You can use this feature to implement auditing or just to know what fields are changed according to the data queried from the persistence:

When activating this feature the application consumes a bit more of memory to keep track of the original values obtained from the persistence. In models that have this feature activated you can check what fields changed as follows:

<?phpuseStore\Toys\Robots;// Get a record from the database$robot=Robots::findFirst();// Change a column$robot->name='Other name';var_dump($robot->getChangedFields());// ['name']var_dump($robot->hasChanged('name'));// truevar_dump($robot->hasChanged('type'));// false

Snapshots are updated on model creation/update. Using hasUpdated() and getUpdatedFields() can be used to check if fields were updated after a create/save/update but it could potentially cause problems to your application if you execute getChangedFields() in afterUpdate(), afterSave() or afterCreate().

Настройка нескольких баз данных

In Phalcon, all models can belong to the same database connection or have an individual one. Actually, when Phalcon\Mvc\Model needs to connect to the database it requests the db service in the application’s services container. You can overwrite this service setting it in the initialize() method:

<?phpusePhalcon\Db\Adapter\Pdo\MysqlasMysqlPdo;usePhalcon\Db\Adapter\Pdo\PostgreSQLasPostgreSQLPdo;// This service returns a MySQL database$di->set('dbMysql',function(){returnnewMysqlPdo(['host'=>'localhost','username'=>'root','password'=>'secret','dbname'=>'invo',]);});// This service returns a PostgreSQL database$di->set('dbPostgres',function(){returnnewPostgreSQLPdo(['host'=>'localhost','username'=>'postgres','password'=>'','dbname'=>'invo',]);});

Then, in the initialize() method, we define the connection service for the model:

But Phalcon offers you more flexibility, you can define the connection that must be used to read and for write. This is specially useful to balance the load to your databases implementing a master-slave architecture:

The ORM also provides Horizontal Sharding facilities, by allowing you to implement a ‘shard’ selection according to the current query conditions:

<?phpnamespaceStore\Toys;usePhalcon\Mvc\Model;classRobotsextendsModel{/**
* Dynamically selects a shard
*
* @param array $intermediate
* @param array $bindParams
* @param array $bindTypes
*/publicfunctionselectReadConnection($intermediate,$bindParams,$bindTypes){// Check if there is a 'where' clause in the selectif(isset($intermediate['where'])){$conditions=$intermediate['where'];// Choose the possible shard according to the conditionsif($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');}}}// Use a default shardreturn$this->getDI()->get('dbShard0');}}

The selectReadConnection() method is called to choose the right connection, this method intercepts any new query executed:

<?phpuseStore\Toys\Robots;$robot=Robots::findFirst('id = 101');

Внедрение сервисов в модели

You may be required to access the application services within a model, the following example explains how to do that:

<?phpnamespaceStore\Toys;usePhalcon\Mvc\Model;classRobotsextendsModel{publicfunctionnotSaved(){// Obtain the flash service from the DI container$flash=$this->getDI()->getFlash();$messages=$this->getMessages();// Show validation messagesforeach($messagesas$message){$flash->error($message);}}}

The notSaved event is triggered every time that a create or update action fails. So we’re flashing the validation messages obtaining the flash service from the DI container. By doing this, we don’t have to print messages after each save.

Отключение/включение возможностей

In the ORM we have implemented a mechanism that allow you to enable/disable specific features or options globally on the fly. According to how you use the ORM you can disable that you aren’t using. These options can also be temporarily disabled if required:

NOTEPhalcon\Mvc\Model::assign() (which is used also when creating/updating/saving model) is always using setters if they exist when have data arguments passed, even when it's required or necessary. This will add some additional overhead to your application. You can change this behavior by adding phalcon.orm.disable_assign_setters = 1 to your ini file, it will just simply use $this->property = value.