zend framework配置操作数据库

2013-01-02 09:02 阅读 505 次 评论关闭

1、

zend framework项目建立后,看了下zend framework配置操作数据库php教程如下:

在application/configs的文件下建立一个config.ini文件

配置信息如下:

[general]
db.adapter=PDO_MYSQL
db.config.host=localhost/IParess      
db.config.username=username
db.config.password=password
db.config.dbname=databasename

2、

在pulibc 目录的index.php页面中

/** Zend_Application */
require_once 'Zend/Application.php';

的下面插入

//set the datase config
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table.php';
$config=new Zend_Config_Ini('./../application/configs/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);

 

就此,我就用我的本地wordpress数据库来测试下,就用wp_posts表来测试吧:

首先模型models建立Wp_posts.php

  1. <?php
  2. class Wp_posts extends Zend_Db_Table{
  3. protected $_name = 'Wp_posts';
  4. protected $_primary = 'ID';
  5. }
  6. ?>

 

控制器controller下面建立IndexController.php

  1. <?php
  2. require_once APPLICATION_PATH.'/models/Wp_posts.php';
  3. class IndexController extends Zend_Controller_Action
  4. {
  5. public function init()
  6. {
  7. /* Initialize action controller here */
  8. }
  9. public function indexAction()
  10. {
  11. $con = new Wp_posts();
  12. $res = $con->fetchAll()->toArray();
  13. $this->view->res = $res;
  14. $this->render("index");
  15. }
  16. }

 

在views/scripts/index/ 建立视图:index.phtml

  1. <html>
  2. <head>
  3. <title>this is for test</title>
  4. </head>
  5. <body>
  6. <table>
  7. <?php foreach ($this->res as $news){?>
  8. <tr>
  9. <td><?php echo $news['id']?></td>
  10. <td><?php echo $news['post_title']?></td>
  11. <td><?php echo $news['post_date']?> </td>
  12. </tr>
  13. <?php }?>
  14. </table>
  15. </body>
  16. </html>
版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:zend framework配置操作数据库 | 猎微网

评论已关闭!