Model Behaviors =============== Behaviors are shared conducts that several models may adopt in order to re-use code, the ORM provides an API to implement behaviors in your models. Also, you can use the events and callbacks as seen before as an alternative to implement Behaviors with more freedom. A behavior must be added in the model initializer, a model can have zero or more behaviors: .. code-block:: php addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => "Y-m-d", ] ] ) ); } } The following built-in behaviors are provided by the framework: +----------------+-------------------------------------------------------------------------------------------------------------------------------+ | Name | Description | +================+===============================================================================================================================+ | Timestampable | Allows to automatically update a model's attribute saving the datetime when a record is created or updated | +----------------+-------------------------------------------------------------------------------------------------------------------------------+ | SoftDelete | Instead of permanently delete a record it marks the record as deleted changing the value of a flag column | +----------------+-------------------------------------------------------------------------------------------------------------------------------+ タイムスタンプ化 ---------------- This behavior receives an array of options, the first level key must be an event name indicating when the column must be assigned: .. code-block:: php addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => "Y-m-d", ] ] ) ); } Each event can have its own options, 'field' is the name of the column that must be updated, if 'format' is a string it will be used as format of the PHP's function date_, format can also be an anonymous function providing you the free to generate any kind timestamp: .. code-block:: php addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => function () { $datetime = new Datetime( new DateTimeZone("Europe/Stockholm") ); return $datetime->format("Y-m-d H:i:sP"); } ] ] ) ); } If the option 'format' is omitted a timestamp using the PHP's function time_, will be used. 論理削除 ---------- This behavior can be used in the following way: .. code-block:: php addBehavior( new SoftDelete( [ "field" => "status", "value" => Users::DELETED, ] ) ); } } This behavior accepts two options: 'field' and 'value', 'field' determines what field must be updated and 'value' the value to be deleted. Let's pretend the table 'users' has the following data: .. code-block:: bash mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | N | +----+---------+--------+ 2 rows in set (0.00 sec) If we delete any of the two records the status will be updated instead of delete the record: .. code-block:: php delete(); The operation will result in the following data in the table: .. code-block:: bash mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | D | +----+---------+--------+ 2 rows in set (0.01 sec) Note that you need to specify the deleted condition in your queries to effectively ignore them as deleted records, this behavior doesn't support that. 独自の振る舞いの作成 --------------------------- The ORM provides an API to create your own behaviors. A behavior must be a class implementing the :doc:`Phalcon\\Mvc\\Model\\BehaviorInterface <../api/Phalcon_Mvc_Model_BehaviorInterface>`. Also, :doc:`Phalcon\\Mvc\\Model\\Behavior <../api/Phalcon_Mvc_Model_Behavior>` provides most of the methods needed to ease the implementation of behaviors. The following behavior is an example, it implements the Blameable behavior which helps identify the user that is performed operations over a model: .. code-block:: php id ); break; default: /* ignore the rest of events */ } } } The former is a very simple behavior, but it illustrates how to create a behavior, now let's add this behavior to a model: .. code-block:: php addBehavior( new Blameable() ); } } A behavior is also capable of intercepting missing methods on your models: .. code-block:: php title); } } } Call that method on a model that implements Sluggable returns a SEO friendly title: .. code-block:: php getSlug(); 振る舞いとしてのトレイトの使用 ------------------------------ Starting from PHP 5.4 you can use Traits_ to re-use code in your classes, this is another way to implement custom behaviors. The following trait implements a simple version of the Timestampable behavior: .. code-block:: php created_at = date("r"); } public function beforeUpdate() { $this->updated_at = date("r"); } } Then you can use it in your model as follows: .. code-block:: php