多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
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. 行为是共享的,为了重用代码,一些模型可能会采用,ORM提供了一个API来实现模型中的行为。另外,您可以使用前面所见的事件和回调,以实现更自由的行为。 A behavior must be added in the model initializer, a model can have zero or more behaviors: 必须在模型初始化器中添加一个行为,一个模型可以有零个或更多的行为: ~~~ <?php use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Behavior\Timestampable; class Users extends Model { public $id; public $name; public $created_at; public function initialize() { $this->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 当创建或更新记录时,允许自动更新一个模型的属性保存datetime | | SoftDelete | Instead of permanently delete a record it marks the record as deleted changing the value of a flag column 不再永久删除记录,而是将记录标记为删除标记列的值 | #### 生成时间戳(Timestampable) This behavior receives an array of options, the first level key must be an event name indicating when the column must be assigned: 该行为接收到一系列选项,第一个级别的键必须是一个事件名称,指示何时必须分配列: ~~~ <?php use Phalcon\Mvc\Model\Behavior\Timestampable; public function initialize() { $this->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: 每个事件都可以有自己的选项,“field”是必须更新的列的名称,如果“format”是一个字符串,它将用作PHP函数日期的格式,格式也可以是一个匿名函数,它可以为您提供任何类型的时间戳: ~~~ <?php use DateTime; use DateTimeZone; use Phalcon\Mvc\Model\Behavior\Timestampable; public function initialize() { $this->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. 如果使用PHP的函数时间来省略选项的“format”,将会使用。 #### 软删除(SoftDelete) This behavior can be used in the following way: 这种行为可以用以下方式来使用: ~~~ <?php use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Users extends Model { const DELETED = "D"; const NOT_DELETED = "N"; public $id; public $name; public $status; public function initialize() { $this->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: 该行为接受两个选项:“field”和“value”,“field”确定必须更新哪些字段,并“value”被删除的值。让我们假设表“users”有以下数据: ~~~ 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: 如果我们删除这两个记录中的任何一个,状态将被更新,而不是删除记录: ~~~ <?php Users::findFirst(2)->delete(); ~~~ The operation will result in the following data in the table: 该操作将导致表中的以下数据: ~~~ 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. 注意,您需要在查询中指定已删除的条件,以有效地忽略它们作为已删除的记录,这种行为不支持这一点。 #### 创建行为(Creating your own behaviors) The ORM provides an API to create your own behaviors. A behavior must be a class implementing the Phalcon\Mvc\Model\BehaviorInterface. Also, Phalcon\Mvc\Model\Behavior provides most of the methods needed to ease the implementation of behaviors. ORM提供了一个API来创建您自己的行为。行为必须是一个实现了 Phalcon\Mvc\Model\BehaviorInterface 接口的类。此外,对于实现行为的实现,Phalcon\Mvc\Model\Behavior行为提供了大部分的方法。 The following behavior is an example, it implements the Blameable behavior which helps identify the user that is performed operations over a model: 下面的行为是一个例子,它实现了可Blameable行为,它帮助识别在模型上执行操作的用户: ~~~ <?php use Phalcon\Mvc\Model\Behavior; use Phalcon\Mvc\Model\BehaviorInterface; class Blameable extends Behavior implements BehaviorInterface { public function notify($eventType, $model) { switch ($eventType) { case "afterCreate": case "afterDelete": case "afterUpdate": $userName = // ... get the current user from session // Store in a log the username, event type and primary key file_put_contents( "logs/blamable-log.txt", $userName . " " . $eventType . " " . $model->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: 前者是一个非常简单的行为,但是它演示了如何创建一个行为,现在让我们将这个行为添加到一个模型中: ~~~ <?php use Phalcon\Mvc\Model; class Profiles extends Model { public function initialize() { $this->addBehavior( new Blameable() ); } } ~~~ A behavior is also capable of intercepting missing methods on your models: 一个行为也可以拦截你的模型中缺失的方法: ~~~ <?php use Phalcon\Tag; use Phalcon\Mvc\Model\Behavior; use Phalcon\Mvc\Model\BehaviorInterface; class Sluggable extends Behavior implements BehaviorInterface { public function missingMethod($model, $method, $arguments = []) { // If the method is 'getSlug' convert the title if ($method === "getSlug") { return Tag::friendlyTitle($model->title); } } } ~~~ Call that method on a model that implements Sluggable returns a SEO friendly title: 在一个实现了“Sluggable”的模型上调用该方法,将会返回一个SEO友好的标题: ~~~ <?php $title = $post->getSlug(); ~~~ #### 使用 Traits 实现行为(Using Traits as behaviors) 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: 从PHP 5.4开始,可以使用Traits在类中重用代码,这是实现自定义行为的另一种方法。下面的Traits实现了一个简单的时间戳行为: ~~~ <?php trait MyTimestampable { public function beforeCreate() { $this->created_at = date("r"); } public function beforeUpdate() { $this->updated_at = date("r"); } } ~~~ Then you can use it in your model as follows: 然后你可以在你的模型中使用它: ~~~ <?php use Phalcon\Mvc\Model; class Products extends Model { use MyTimestampable; } ~~~