zendframework 2 链接数据库
相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,但是对数据库的操作虽然配置写好的就基本不需要动了,但是还是比1的配置要繁琐,
还是那句话,大家可以去看看源码。。。
1 Module.php 里面添加
public function getServiceConfig() 2 { 3 return array( 4 'factories' => array( 5 'Student\Model\StudentTable' => function($sm) { 6 $tableGateway = $sm->get('StudentTableGateway'); 7 $table = new StudentTable($tableGateway); 8 return $table; 9 }, 10 'StudentTableGateway' => function ($sm) { 11 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 12 $resultSetPrototype = new ResultSet(); 13 $resultSetPrototype->setArrayObjectPrototype(new Student()); 14 return new TableGateway('cc_user', $dbAdapter, null, $resultSetPrototype);//table Name is cc_user 15 }, 16 ), 17 ); 18 }
1 namespace Student\Model; 2 3 class Student 4 { 5 public $id; 6 public $name; 7 public $phone; 8 public $mark; 9 public $email; 10 11 public function exchangeArray($data)//别名 12 { 13 $this->id = (!empty($data['cc_u_id'])) ? $data['cc_u_id'] : null; 14 $this->name = (!empty($data['cc_u_name'])) ? $data['cc_u_name'] : null; 15 $this->phone = (!empty($data['cc_u_phone'])) ? $data['cc_u_phone'] : null; 16 $this->mark = (!empty($data['cc_u_mark'])) ? $data['cc_u_mark'] : null; 17 $this->email = (!empty($data['cc_u_email'])) ? $data['cc_u_email'] : null; 18 } 19 }
1 <?php 2 namespace Student\Model; 3 4 use Zend\Db\ResultSet\ResultSet; 5 use Zend\Db\TableGateway\TableGateway; 6 use Zend\Db\Sql\Select; 7 use Zend\Paginator\Adapter\DbSelect; 8 use Zend\Paginator\Paginator; 9 10 class StudentTable 11 { 12 protected $tableGateway; 13 protected $table='cc_user'; 14 15 public function __construct(TableGateway $tableGateway) 16 { 17 $this->tableGateway = $tableGateway; 18 } 19 20 public function fetchAll($paginated) 21 {//分页 22 if($paginated) { 23 // create a new Select object for the table album 24 $select = new Select('cc_user'); 25 // create a new result set based on the Student entity 26 $resultSetPrototype = new ResultSet(); 27 $resultSetPrototype->setArrayObjectPrototype(new Student()); 28 // create a new pagination adapter object 29 $paginatorAdapter = new DbSelect( 30 // our configured select object 31 $select, 32 // the adapter to run it against 33 $this->tableGateway->getAdapter(), 34 // the result set to hydrate 35 $resultSetPrototype 36 ); 37 $paginator = new Paginator($paginatorAdapter); 38 return $paginator; 39 } 40 $resultSet = $this->tableGateway->select(); 41 return $resultSet; 42 } 43 44 public function getStudent($id) 45 { 46 $id = (int) $id; 47 $rowset = $this->tableGateway->select(array('id' => $id)); 48 $row = $rowset->current(); 49 if (!$row) { 50 throw new \Exception("Could not find row $id"); 51 } 52 return $row; 53 } 54 55 56 public function deleteStudent($id) 57 { 58 $this->tableGateway->delete(array('id' => $id)); 59 } 60 61 public function getLIValue(){ 62 return $this->tableGateway->getLastInsertValue(); 63 } 64 65 66 }
1 Student/IndexController.php 调用数据库
public function indexAction(){ 2 /* return new ViewModel(array( 3 'students' => $this->getStudentTable()->fetchAll(), //不分页 4 ));*/ 5 $page=$this->params('page');//走分页 在model.config.php里面设置
1 model.config.php
'defaults' => array( 2 'controller' => 'Student\Controller\Index', 3 'action' => 'index', 4 'page'=>'1', 5 ),
6 $paginator = $this->getStudentTable()->fetchAll(true); 7 // set the current page to what has been passed in query string, or to 1 if none set 8 $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', $page)); 9 // set the number of items per page to 10 10 $paginator->setItemCountPerPage(10); 11 12 return new ViewModel(array( 13 'paginator' => $paginator //模板页面调用的时候的名字 14 )); 15 //print_r($this->getStudentTable()->fetchAll()); 16 17 }
1在模板页面的调用
<?php foreach ($this->paginator as $student) : ?> 2 <tr id="<?php echo $this->escapeHtml($student->id);?>"> 3 <td><?php echo $this->escapeHtml($student->id);?></td> 4 <td><?php echo $this->escapeHtml($student->name);?></td> 5 <td><?php echo $this->escapeHtml($student->phone);?></td> 6 <td><?php echo $this->escapeHtml($student->email);?></td>//应用了在Student.php的别名 7 <td><?php echo $this->escapeHtml($student->mark);?></td> 8 <td><a href='#' class='icol-bandaid editUserInfo'></a> 9 <a href='#' class='icol-key changePwd'></a> 10 <a herf='#' class='icol-cross deleteStud'></a> 11 </td> 12 </tr> 13 <?php endforeach;?>