Cakephp 3 - Table 实体的可重用代码
Cakephp 3 - reusable code for Table Entities
我有一些代码,我需要申请多个表的实体
类似于这里的例子
http://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators
protected function _setTitle($title)
{
// code to make re-usable
return $title;
}
我可以在哪里移动我的代码,以便我可以从多个实体访问它。我在 Behavior 中尝试了一个函数,但它没有用。
谢谢
您可以选择以下两种方法之一。首先,使用特征(有点像你试图通过行为实现的):-
class Example extends Entity
{
use TitleTrait;
}
trait TitleTrait
{
protected function _setTitle($title)
{
return $title;
}
}
第二种方法是使用继承:-
class Example extends CustomEntity
{
}
abstract class CustomEntity extends Entity
{
protected function _setTitle($title)
{
return $title;
}
}
我有一些代码,我需要申请多个表的实体
类似于这里的例子 http://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators
protected function _setTitle($title)
{
// code to make re-usable
return $title;
}
我可以在哪里移动我的代码,以便我可以从多个实体访问它。我在 Behavior 中尝试了一个函数,但它没有用。
谢谢
您可以选择以下两种方法之一。首先,使用特征(有点像你试图通过行为实现的):-
class Example extends Entity
{
use TitleTrait;
}
trait TitleTrait
{
protected function _setTitle($title)
{
return $title;
}
}
第二种方法是使用继承:-
class Example extends CustomEntity
{
}
abstract class CustomEntity extends Entity
{
protected function _setTitle($title)
{
return $title;
}
}