Documentation

模型行为(Model Behaviors)

行为是几个模型可以使用的共享行为,以便重新使用代码,ORM提供了一个API来实现模型中的行为。 此外,您可以使用的事件和回调作为实现行为的更方便的替代方法。

行为必须在模型初始化器中添加,一个模型中可以有零个或多个行为:

<?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(
                array(
                    'beforeCreate' => array(
                        'field'  => 'created_at',
                        'format' => 'Y-m-d'
                    )
                )
            )
        );
    }
}

The following built-in behaviors are provided by the framework:

Name Description
Timestampable 允许自动更新模型的属性,以便在创建或更新记录时保存日期时间
SoftDelete 它将记录标记为已删除,而不是真实删除记录

生成时间戳(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(
            array(
                'beforeCreate' => array(
                    '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:

<?php

use Phalcon\Mvc\Model\Behavior\Timestampable;

public function initialize()
{
    $this->addBehavior(
        new Timestampable(
            array(
                'beforeCreate' => array(
                    '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.

软删除(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(
                array(
                    '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:

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.

The following behavior is an example, it implements the Blameable behavior which helps identify the user that is performed operations over a model:

<?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 = array())
    {
        // 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:

<?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

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;
}