方便的模型内消毒
在基础模型中设置便捷方法
namespace Base;
class Model extends \Phalcon\Mvc\Model
{
public function sanitize($attr, $filterName)
{
$filter = $this->getDI()->get('filter');
$this->$attr = $filter->sanitize($this->$attr, $filterName);
}
}
然后像这样使用
class User extends \Base\Model
{
public function beforeValidation()
{
$this->sanitize('id', 'int');
// input $this->id: 123abc
// output: 123
$this->sanitize('email', 'email');
// input $this->email: youre(-)mail@dom/ain.com
// output: youremail@domain.com
$this->sanitize('wage', 'float');
// input $this->wage: +1234ab.56cd
// output: 1234.56
$this->sanitize('name', 'string');
// input $this->name: <john>
// output: john
}
}