下面创建一个链接数据库的开发实例
来源:互联网 发布:淘宝联盟的发单机器人 编辑:程序博客网 时间:2024/05/17 07:31
1、创建controller - IndexController.php
<?php/* * To change this template, choose Tools | Templates * and open the template in the editor. */namespace home\Controller;use Zend\Mvc\Controller\AbstractActionController;use Zend\View\Model\ViewModel;use home\Model\Artical;use home\Form\ArticalForm;use Zend\Session\SessionManager as Zend_Session_SessionManager;class IndexController extends AbstractActionController{ protected $articalModel; public function getArticalModel() { if (!$this->articalModel) { $this->articalModel = $this-> getServiceLocator() ->get('home\Model\ArticalModel'); } return $this->articalModel; } public function indexAction(){ $form = new ArticalForm(); $form ->get('submit')->setAttribute('value', '提交'); $request = $this->getRequest(); if($request -> isPost()){ $artical = new Artical(); $form ->setInputFilter($artical -> getInputFilter()); $form ->setData($request->getPost()); if($form ->isValid()){ $artical -> exchangeArray($form -> getData()); $this->getArticalModel()->saveArtical($artical); return $this->redirect()->toRoute(); } } $where = array(); $order = array(); $count =$this-> getArticalModel() -> getCount(); $pagesize = 10; $nowpagenum = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1 ; $page = $this->PageNum($count, $pagesize ,$nowpagenum); $limit = array('limit' => ($nowpagenum-1)*$pagesize.','.$pagesize); $list = $this->getArticalModel() ->fetchAll($where,$order,$limit); return array( 'form' => $form, 'list' => $list, 'page' => $page, ); } }2、创建FORM表单相关内容 -ArticalForm.php
<?php/* * To change this template, choose Tools | Templates * and open the template in the editor. */namespace home\Form;use Zend\Form\Form;class ArticalForm extends Form{ public function __construct($name=null){ parent::__construct('artical'); $this->setAttribute('method', 'post'); $this->add(array( 'name' => 'id', 'attributes' => array( 'type' => 'hidden', ), )); $this->add(array( 'name' => 'title', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => '标题', ), )); $this->add(array( 'name' => 'content', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => '内容', ), )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit', 'value' => '提交', 'id' => 'submitbutton', ), )); } }?>
3、创建Model-Artical.php以及数据库操作Model-ArticalModel.php
Artical.php代码如下:
<?phpnamespace home\Model;use Zend\InputFilter\InputFilter;use Zend\InputFilter\Factory as InputFactory;use Zend\InputFilter\InputFilterAwareInterface;use Zend\InputFilter\InputFilterInterface;class Artical implements InputFilterAwareInterface{ public $id; public $titile; public $content; protected $inputFilter; public function exchangeArray($data){ $this->id = (isset($data['id'])) ? $data['id'] : null; $this->title = (isset($data['title'])) ? $data['title'] : null; $this->content = (isset($data['content'])) ? $data['content'] : null; } public function getArrayCopy(){ return get_object_vars($this); } public function setInputFilter(InputFilterInterface $inputFilter){ throw new Exception('Not Used!'); } public function getInputFilter(){ if(!$this->inputFilter){ $inputFilter = new InputFilter(); $factory =new InputFactory(); $inputFilter ->add($factory ->createInput(array( 'name' => 'id', 'required' => true, 'filters' => array( array('name' => 'Int'), ) ))); $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' => 1, 'max' => 100, ), ), ), ))); $inputFilter ->add($factory ->createInput(array( 'name' => 'content', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, ), ), ), ))); $this->inputFilter = $inputFilter; } return $this->inputFilter; } }?>
ArticalModel.php代码如下:
<?phpnamespace home\Model;use Zend\Db\TableGateway\AbstractTableGateway;use Zend\Db\Adapter\Adapter;use Zend\Db\ResultSet\ResultSet;class ArticalModel extends AbstractTableGateway{ protected $table = 'artical'; public function __construct(Adapter $adapter){ $this -> adapter = $adapter; $this -> resultSetPrototype = new ResultSet(); $this -> resultSetPrototype -> setArrayObjectPrototype(new Artical()); $this -> initialize(); } public function fetchAll($where,$order,$limit){ $resultSet = $this -> dbSelect($where,$order,$limit); return $resultSet; } public function getCount(){ $result = $this->dbCount(); return $result[0]; } public function getArtical($id){ $id = (int) $id; $rowSet = $this -> select(array('id'=>$id)); $rows = $rowSet -> current(); if(!$rows){ throw new Exception("Could not find row {$id}"); } return $rows; } public function saveArtical(Artical $artical){ $data = array( 'title' => $artical -> title, 'content' => $artical -> content ); $id = (int) $artical -> id; if($id == 0){ $this->insert($data); }else{ if($this->getArtical($id)){ $this->update($data,array('id'=>$id)); }else{ throw new Exception('Form Id is not find!'); } } } public function deleteArtical($id){ $this->delete(array('id'=>$id)); } public function count(){ } }?>
4、view部分
zf生成表单部分:
<?php $form = $this -> form ; $form ->setAttribute('action', $this -> url('home',array('action' => 'index'))); $form ->prepare(); echo $this->form()->openTag($form); echo $this->formHidden($form->get('id')); echo $this->formRow($form->get('title')); echo $this->formRow($form->get('content')); echo $this->formInput($form->get('submit')); echo $this->form()->closeTag($form);?>
页面显示部分:
<?php echo '<table>'; echo '<tr><th>序号</th><th>标题</th><th>内容</th></tr>'; for($i=0;$i<count($this->list);$i++){ echo '<tr><td>'.$this->list[$i][0].'</td><td>'.$this->list[$i][1].'</td><td>'.$this->list[$i][2].'</td></tr>'; } echo '</table>'; echo $this->page;?>
5、模块根目录下Module.php配置部分:
<?php/** * @todo 配置该模块中使用到得数据库模型 * @return String $table */namespace home;use home\Model\ArticalModel;class Module{ public function getAutoloaderConfig() { return array( 'Zend\Loader\ClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php', ), 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } public function getServiceConfig() { return array( 'factories' => array( 'home\Model\ArticalModel' => function($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $table = new ArticalModel($dbAdapter); return $table; }, ), ); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } }
由此就完成了一个链接数据库的发布文章的ZF开发实例。
0 0
- 下面创建一个链接数据库的开发实例
- 创建一个JDBC链接数据库的程序
- MYSQL|ORACLE的数据库链接实例______软件开发-数据库
- 一个简单的java链接oracle数据库实例
- 下面是一个SimpleFormController的实例
- 一个oracle 用户下面创建多个实例
- 一个oracle 用户下面创建多个实例
- 一个oracle 用户下面创建多个实例
- oracle 数据库在一个schema下面创建另外一个schema的表上的trigger的权限
- 一个oracle 数据库创建多个实例
- fullcalendar创建的一个实例
- sys下面 proc下面,dev下面分别创建一个节点
- 创建一个以JDBC链接数据库的程序,包含7个步骤
- 从drawable下面的图片 创建一个Bitmap
- 创建一个链接的服务器 - sp_addlinkedserver
- ADO 实例《创建一个临时数据库 创建一个临时数据表 GridView1绑定内容 以及Dataset_的使用》
- ajax链接数据库实例
- oracle 创建数据库 在新创建的数据库中创建用户 删除创建数据库 链接orcl
- Android XML解析、序列化demo
- dedecms织梦文章内容图片显示绝对路径修改
- <Android API Guide学习记录> -- Activities
- hbase常见错误
- SQL基础笔记
- 下面创建一个链接数据库的开发实例
- windows phone导航到WIFI BlueTool Cellular AirplaneMode设置界面
- 大整数相加链表实现(Add Two Numbers)
- Cocos2d-x layout (二)
- C++ const对象 和 C const对象的区别之一 —— 默认作用域
- 触摸屏驱动三部曲之硬件原理
- Hosts文件被覆盖了怎么恢复
- LCA HDU 4297 One and One Story
- poj 1466 最大独立集