相对于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_user15 },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 tableGateway = $tableGateway;18 }19 20 public function fetchAll($paginated)21 { //分页22 if($paginated) {23 // create a new Select object for the table album24 $select = new Select('cc_user');25 // create a new result set based on the Student entity26 $resultSetPrototype = new ResultSet();27 $resultSetPrototype->setArrayObjectPrototype(new Student());28 // create a new pagination adapter object29 $paginatorAdapter = new DbSelect(30 // our configured select object31 $select,32 // the adapter to run it against33 $this->tableGateway->getAdapter(),34 // the result set to hydrate35 $resultSetPrototype36 );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 1010 $paginator->setItemCountPerPage(10);11 12 return new ViewModel(array(13 'paginator' => $paginator //模板页面调用的时候的名字14 ));15 //print_r($this->getStudentTable()->fetchAll());16 17 }
1在模板页面的调用 paginator as $student) : ?> 2 3 escapeHtml($student->id);?> 4 escapeHtml($student->name);?> 5 escapeHtml($student->phone);?> 6 escapeHtml($student->email);?>//应用了在Student.php的别名 7 escapeHtml($student->mark);?> 8 9 10 11 12 13