1.新建数据库
create database testzf;
[mysql]
db.adapter=PDO_MYSQL
db.params.host=localhost
db.params.username=root
db.params.password=123456
db.params.dbname=test
2.创建表
create table message(
message_id int(10) unsigned not null auto_increment,
sender varchar(64) not null,
getter varchar(64) not null,
sendtime datetime not null,
content varchar(2000) not null,
primary key (message_id)
)engine INNODB default charset=utf8
3.创建模型
<?php
//这个类和数据库中的某种表对应,通过Message对象实例可以完成对表的CRUD操作
class Message extends Zend_Db_Table
{
protected $_name = 'emp';
protected $_primary = 'emp_id';
}
引入:
<?php
require_once APPLICATION_PATH . '/models/Message.php';
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
echo __METHOD__;
}
public function indexAction()
{
// action body
echo __METHOD__,'<br/>';
$messageModel = new Message();
$res = $messageModel->fetchAll()->toArray();
var_dump($res);
//怎样把控制器中获取的数据
//$this->view固定的,后面的值,随意
$this->view->res = $res;
}
}
4.完成配置
5.初始化数据库适配器
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function __construct($app)
{
parent::__construct($app);
echo __METHOD__,'<br/>';
//初始化我们的数据库适配器
$url = constant('APPLICATION_PATH') . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . 'application.ini';
$dbconfig = new Zend_Config_Ini($url,'mysql');
$db = Zend_Db::factory($dbconfig->db);
$db->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($db);
}
}
6.在模板中输出