如何扩展 Table Class CakePhp3
How to Extended Table Class CakePhp3
我需要扩展 ModelTable class 来添加 follow 方法一次,而不是每次都添加 class
public function addOrUpdate($data, $options=[]) {
if ($this->existe($data)) {
$obj = $this->findById($data['id'])->first();
$obj = $this->patchEntity($obj, $data,$options);
}else{
$obj = $this->newEntity($data,$options);
}
return $this->save($obj);
}
您可以创建自定义Table class 扩展 Table class。将您的函数放在 CustomTable class.Then 中,您可以创建每个模型 table class 扩展 CustomTable class。
class CustomTable extends Table {
public function addOrUpdate($data, $options = [])
{
...
}
}
class ModelTable extends CustomTable {
...
}
我需要扩展 ModelTable class 来添加 follow 方法一次,而不是每次都添加 class
public function addOrUpdate($data, $options=[]) {
if ($this->existe($data)) {
$obj = $this->findById($data['id'])->first();
$obj = $this->patchEntity($obj, $data,$options);
}else{
$obj = $this->newEntity($data,$options);
}
return $this->save($obj);
}
您可以创建自定义Table class 扩展 Table class。将您的函数放在 CustomTable class.Then 中,您可以创建每个模型 table class 扩展 CustomTable class。
class CustomTable extends Table {
public function addOrUpdate($data, $options = [])
{
...
}
}
class ModelTable extends CustomTable {
...
}