數據庫表基類:php
<?php
namespace Oa\Model;
use \Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class BaseModel implements InputFilterAwareInterface
{
protected $inputFilter;
public function exchangeArray($data)
{
$properties = $this->getArrayCopy();
foreach ($properties as $key => $value) {
if($key != 'inputFilter') {
$this->$key = (!empty($data[$key])) ? $data[$key] : null;
}
}
}
public function getArrayCopy(){
return get_object_vars($this);
}
public function getInputFilter() {
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception('Not used');
}
}
實現類:git
<?php
namespace Oa\Model;
use \Zend\InputFilter\InputFilterAwareInterface;
use \Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory;
use Zend\InputFilter\InputFilterInterface;
use Oa\Model\BaseModel;
class Userinfo extends BaseModel
{
public $id;
public $employeeId;
public $name;
public $company;
public $area;
public $part1;
public $part2;
public $team;
public $job;
public $attendId;
public $identify;
public $sex;
public function getInputFilter() {
if(!$this->inputFilter) {
$this->inputFilter = new InputFilter();
$this->inputFilter->add([
'name'=>'employeeId',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'員工號不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'attendId',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'考勤號不能爲空',
],
],
],
['name'=>'Digits',
'options'=>[
'message'=>[
'\Zend\Validator\Digits::NOT_DIGITS'=>'考勤號只能爲數字'
]
]
]
]
]);
$this->inputFilter->add([
'name'=>'identify',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'身份證不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'name',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'姓名不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'name',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'姓名不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'area',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'地區不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'part1',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'部門不能爲空',
],
],
],
]
]);
$this->inputFilter->add([
'name'=>'company',
'required'=>true,
'filters'=>[
['name'=>'StringTrim'],
],
'validators'=>[
['name'=>'NotEmpty',
'options'=>[
'message'=>[
'\Zend\Validator\NotEmpty::IS_EMPTY'=>'公司不能爲空',
],
],
],
]
]);
}
return $this->inputFilter;
}
}
都是本身摸索着寫的,代碼有不少地方寫得不夠好,還請各位同行指教!!!數據庫