7.5 添加模型文件
最后更新于:2022-04-01 00:41:24
模型是ZF2对数据库操作的核心内容,也是进行数据过滤、数据交换的功能专区。
### 7.5.1 添加 Album.php
此文件包括数据交换、表单数据过滤功能;添加 `/module/Album/src/Album/Model/Album.php` 内容如下:
~~~
namespace Album\Model;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Album implements InputFilterAwareInterface {
public $id;
public $artist;
public $title;
protected $inputFilter;
public function exchangeArray($data){
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
public function getArrayCopy(){
return get_object_vars($this);
}
public function getInputFilter() {
if(!$this->inputFilter){
$this->inputFilter = new InputFilter();
$factory = new InputFactory();
$this->inputFilter->add($factory->createInput(array(
'name'=>'id',
'required'=>true,
'filters'=>array(
array('name'=>'Int'),
),
)));
$this->inputFilter->add($factory->createInput(array(
'name'=>'artist',
'required'=>true,
'filters'=>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators'=>array(
array(
'name'=>'StringLength',
'options'=>array(
'encoding'=>'UTF-8',
'min'=>5,
'max'=>100,
),
),
),
)));
$this->inputFilter->add($factory->createInput(array(
'name'=>'title',
'required'=>true,
'filters'=>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators'=>array(
array(
'name'=>'StringLength',
'options'=>array(
'encoding'=>'UTF-8',
'min'=>5,
'max'=>100,
),
),
),
)));
}
return $this->inputFilter;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception('Not used');
}
}
~~~
代码解释:
public function exchangeArray($data){} 数据转换
public function getArrayCopy(){} 克隆对象内属性
public function getInputFilter() {} 过滤器
### 7.5.2 添加AlbumTable.php
此文件为数据库操作网关,实现对数据库的一系列操作;添加文件:`/module/Album/src/Album/Model/AlbumTable.php`,具体内容如下:
~~~
namespace Album\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
class AlbumTable {
protected $tableGateway;
public function __construct(TableGateway $tg)
{
$this->tableGateway = $tg;
}
public function fetchAll($paginated=false)
{
if($paginated){// 分页
$select = new Select('album');
$rs = new ResultSet();
$rs->setArrayObjectPrototype(new Album());
$pageAdapter = new DbSelect($select,$this->tableGateway->getAdapter(),$rs);
$paginator = new Paginator($pageAdapter);
return $paginator;
}
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getAlbum($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id'=>$id));
$row = $rowset->current();
if(!$row){
throw new \Exception("Could not find row {$id}");
}
return $row;
}
public function saveAlbum(Album $album)
{
$data = array(
'artist' =>$album->artist,
'title' =>$album->title
);
$id = (int) $album->id;
if($id == 0){
$this->tableGateway->insert($data);
}else{
if($this->getAlbum($id)){
$this->tableGateway->update($data,array('id'=>$id));
}else{
throw new \Exception("Could not find row {$id}");
}
}
}
public function deleteAlbum($id)
{
$this->tableGateway->delete(array('id'=>$id));
}
}
~~~
代码解释:
public function fetchAll($paginated=false){} 获取数据表中的所有记录
public function getAlbum($id){} 获取指定ID的记录行
public function saveAlbum(Album $album){} 保存数据到数据库
public function deleteAlbum($id){} 删除指定ID的记录行