ThinkPHP 5 模型使用历程 - 复写
TP 提供的功能很多,很通用,但不一定适合自己,需要自己 “变通”
比如在查询结果的 model 对象上获取数据表字段值,经常报错会很烦,我就把它复写了:
class MTBase extends Model
{
/**
* 捕获所有字段不存在的异常,返回空值
*
* @param string $name
* @param null $item
*
* @return mixed|string
*/
public function getAttr($name, &$item = null)
{
try {
$value = parent::getAttr($name);
} catch (InvalidArgumentException $e) {
return '';
} catch (Exception $e) {
return $e->getMessage();
}
return $value;
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: