加载中...

7.5 添加模型文件


模型是ZF2对数据库操作的核心内容,也是进行数据过滤、数据交换的功能专区。

7.5.1 添加 Album.php

此文件包括数据交换、表单数据过滤功能;添加 /module/Album/src/Album/Model/Album.php 内容如下:

  1. namespace Album\Model;
  2. use Zend\InputFilter\Factory as InputFactory;
  3. use Zend\InputFilter\InputFilter;
  4. use Zend\InputFilter\InputFilterAwareInterface;
  5. use Zend\InputFilter\InputFilterInterface;
  6. class Album implements InputFilterAwareInterface {
  7. public $id;
  8. public $artist;
  9. public $title;
  10. protected $inputFilter;
  11. public function exchangeArray($data){
  12. $this->id = (isset($data['id'])) ? $data['id'] : null;
  13. $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
  14. $this->title = (isset($data['title'])) ? $data['title'] : null;
  15. }
  16. public function getArrayCopy(){
  17. return get_object_vars($this);
  18. }
  19. public function getInputFilter() {
  20. if(!$this->inputFilter){
  21. $this->inputFilter = new InputFilter();
  22. $factory = new InputFactory();
  23. $this->inputFilter->add($factory->createInput(array(
  24. 'name'=>'id',
  25. 'required'=>true,
  26. 'filters'=>array(
  27. array('name'=>'Int'),
  28. ),
  29. )));
  30. $this->inputFilter->add($factory->createInput(array(
  31. 'name'=>'artist',
  32. 'required'=>true,
  33. 'filters'=>array(
  34. array('name'=>'StripTags'),
  35. array('name'=>'StringTrim'),
  36. ),
  37. 'validators'=>array(
  38. array(
  39. 'name'=>'StringLength',
  40. 'options'=>array(
  41. 'encoding'=>'UTF-8',
  42. 'min'=>5,
  43. 'max'=>100,
  44. ),
  45. ),
  46. ),
  47. )));
  48. $this->inputFilter->add($factory->createInput(array(
  49. 'name'=>'title',
  50. 'required'=>true,
  51. 'filters'=>array(
  52. array('name'=>'StripTags'),
  53. array('name'=>'StringTrim'),
  54. ),
  55. 'validators'=>array(
  56. array(
  57. 'name'=>'StringLength',
  58. 'options'=>array(
  59. 'encoding'=>'UTF-8',
  60. 'min'=>5,
  61. 'max'=>100,
  62. ),
  63. ),
  64. ),
  65. )));
  66. }
  67. return $this->inputFilter;
  68. }
  69. public function setInputFilter(InputFilterInterface $inputFilter) {
  70. throw new \Exception('Not used');
  71. }
  72. }

代码解释:

public function exchangeArray($data){} 数据转换

public function getArrayCopy(){} 克隆对象内属性

public function getInputFilter() {} 过滤器

7.5.2 添加AlbumTable.php

此文件为数据库操作网关,实现对数据库的一系列操作;添加文件:/module/Album/src/Album/Model/AlbumTable.php,具体内容如下:

  1. namespace Album\Model;
  2. use Zend\Db\TableGateway\TableGateway;
  3. use Zend\Db\ResultSet\ResultSet;
  4. use Zend\Db\Sql\Select;
  5. use Zend\Paginator\Adapter\DbSelect;
  6. use Zend\Paginator\Paginator;
  7. class AlbumTable {
  8. protected $tableGateway;
  9. public function __construct(TableGateway $tg)
  10. {
  11. $this->tableGateway = $tg;
  12. }
  13. public function fetchAll($paginated=false)
  14. {
  15. if($paginated){// 分页
  16. $select = new Select('album');
  17. $rs = new ResultSet();
  18. $rs->setArrayObjectPrototype(new Album());
  19. $pageAdapter = new DbSelect($select,$this->tableGateway->getAdapter(),$rs);
  20. $paginator = new Paginator($pageAdapter);
  21. return $paginator;
  22. }
  23. $resultSet = $this->tableGateway->select();
  24. return $resultSet;
  25. }
  26. public function getAlbum($id)
  27. {
  28. $id = (int) $id;
  29. $rowset = $this->tableGateway->select(array('id'=>$id));
  30. $row = $rowset->current();
  31. if(!$row){
  32. throw new \Exception("Could not find row {$id}");
  33. }
  34. return $row;
  35. }
  36. public function saveAlbum(Album $album)
  37. {
  38. $data = array(
  39. 'artist' =>$album->artist,
  40. 'title' =>$album->title
  41. );
  42. $id = (int) $album->id;
  43. if($id == 0){
  44. $this->tableGateway->insert($data);
  45. }else{
  46. if($this->getAlbum($id)){
  47. $this->tableGateway->update($data,array('id'=>$id));
  48. }else{
  49. throw new \Exception("Could not find row {$id}");
  50. }
  51. }
  52. }
  53. public function deleteAlbum($id)
  54. {
  55. $this->tableGateway->delete(array('id'=>$id));
  56. }
  57. }

代码解释:

public function fetchAll($paginated=false){} 获取数据表中的所有记录

public function getAlbum($id){} 获取指定ID的记录行

public function saveAlbum(Album $album){} 保存数据到数据库

public function deleteAlbum($id){} 删除指定ID的记录行


还没有评论.