<?php<364666827@qq.com>,开发者QQ群:50304283namespace app\example\model;
use think\Model;
use think\Loader;
class ExampleNews extends Model
{
protected $createTime = 'ctime';
protected $updateTime = 'mtime';
protected $autoWriteTimestamp = true;
public function getContentAttr($value)
{
return htmlspecialchars_decode($value);
}
public function hasCategory()
{
return $this->hasOne('ExampleCategory', 'id', 'cid');
}
/**
* 入库
* @param array $data 入库数据
* @author 橘子俊 <364666827@qq.com>
* @return bool
*/
public function storage($data = [])
{
if (empty($data)) {
$data = request()->post();
}
$valid = Loader::validate('ExampleNews');
if($valid->check($data) !== true) {
$this->error = $valid->getError();
return false;
}
if (isset($data['id']) && !empty($data['id'])) {
$res = $this->update($data);
} else {
$res = $this->create($data);
}
if (!$res) {
$this->error = '保存失败';
return false;
}
return $res;
}
}