Model Behaviors
Behaviors are shared constructs 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:
The following built-in behaviors are provided by the framework:
This behavior receives an array of options, the first level key must be an event name indicating when the column must be assigned:
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:
<?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');
},
],
]
)
);
}
This behavior can be used as follows:
<?php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
{
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:
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.
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\ModelInterface;
use Phalcon\Mvc\Model\Behavior;
class Blameable extends Behavior
{
public function notify(string $eventType, ModelInterface $model)
{
switch ($eventType) {
case 'afterCreate':
case 'afterDelete':
$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:
A behavior is also capable of intercepting missing methods on your models:
<?php
use Phalcon\Tag;
use Phalcon\Mvc\ModelInterface;
use Phalcon\Mvc\Model\Behavior;
use Phalcon\Mvc\Model\BehaviorInterface;
class Sluggable extends Behavior
{
public function missingMethod(string $model, ModelInterface $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:
<?php
$title = $post->getSlug();
You can use 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');