Hi, I'm using PHP traits. The problem is that I don't know the proper way to register my trait with phalcon. What I did until now is include the trait file in to index.php and it's works.
Is there a way to register the trait with phalcon DI?
My index.php file
<?php
error_reporting(E_ALL);
try {
$config = include __DIR__ . "/../app/config/config.php";
include __DIR__ . "/../app/config/services.php";
// include trait file
include __DIR__ . "/../app/modules/v1/models/helpers/ModelsTraits.php";
$application = new \Phalcon\Mvc\Application($di);
$application->registerModules(['v1' => ['className' => 'Yapi\V1\Module', 'path' => '../app/modules/v1/Module.php']]);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo get_class($e), " : <br><span style='color:red'>";
echo $e->getMessage(), "</span><br><hr>";
echo " File : ", $e->getFile(), "<br>";
echo " Line : ", $e->getLine(), "<br><pre style='color:blue'>";
echo $e->getTraceAsString(), '</pre>';
echo $e->getMessage();
}
my trait file
<?php
namespace Yapi\V1\Models\Helpers;
trait CreatedUpdatedAt
{
public function beforeValidationOnCreate()
{
$this->updated_at = $this->created_at = date("Y-m-d H:i:s");
}
public function beforeValidationOnUpdate()
{
$this->updated_at = date("Y-m-d H:i:s");
}
}
and the usage of the trait in the model class
<?php
namespace Yapi\V1\Models\Entities;
class Category extends \Phalcon\Mvc\Model
{
use \Yapi\V1\Models\Helpers\CreatedUpdatedAt;
/**
*
* @var integer
*/
public $id;
/**
*
* @var string
*/
public $category;
/**
*
* @var integer
*/
public $parent_category;
/**
*
* @var string
*/
public $created_at;
/**
*
* @var string
*/
public $updated_at;
public function initialize()
{
$this->hasMany('id', 'Yapi\V1\Models\Entities\MapCategoryProduct', 'category_id', ['alias' => 'mapCategoryProduct']);
$this->hasMany('id', 'Yapi\V1\Models\Entities\Coupon', 'category_id', ['alias' => 'coupon']);
}
public function getSource()
{
return 'category';
}
}