Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408212
  • 博文数量: 117
  • 博客积分: 5235
  • 博客等级: 大校
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-12 15:51
文章分类

全部博文(117)

文章存档

2012年(9)

2011年(2)

2010年(21)

2009年(13)

2008年(72)

我的朋友

分类:

2010-03-31 10:04:44

1. 简介
各种数据库操作接口
2.
===最简单的连接
$para2 = array(
     'host'     => '127.0.0.1',
     'username' => 'jack',
     'password' => '111111',
     'dbname'   => 'zftest',
     'port'       => '3306',
  );
  $this->db = Zend_Db::factory('PDO_MYSQL',$para2);
===
3. Zend_Db_Table 表的抽象
//设置Zend_Db_Table的默认适配器
Zend_Db_Table::setDefaultAdapter($this->db);
//实现表的抽象,一般在models目录下
Student.php
===
class Student extends Zend_Db_Table {
    //重定义表名和主键
    protected function _setup()
    {
        $this->_name = 'student';
        $this->_primary = 'id';
        parent::_setup();
    }
    //定义一些方法操作表,或过虑参数
    public function add($data){
     $this->insert($data);
    }
}
===
4. 使用
//将Student类加载进来,之前要在index.php中设置好include的目录models
Zend_Loader::loadClass('Student');
//实例化$db
//设置默认适配器
Student::setDefaultAdapter($this->db);
//实例化表类
$table = new Student();
5. 操作
5.1 insert
5.2 update
===
$set = array('name' => 'aaa',
     'password' => md5('111111')
  );
  $whereAsk = "id = ?";
  $whereValue = 1;
$table->updateRow($set, $whereAsk, $whereValue);
public function updateRow($set, $whereAsk, $whereValue){
     $db = $this->getAdapter();
     $where = $db->quoteInto($whereAsk, $whereValue);
     $this->update($set, $where);
}
===
5.3 delete
   public function deleteRow($whereAsk, $whereValue){
     $db = $this->getAdapter();
     $where = $db->quoteInto($whereAsk, $whereValue, "int");
     $this->delete($where);
    }
5.4 show
===rowset结构
 public function showAction()
    {
     echo "Show";
     //where语句, order by name, limit 1, 2
     $rowset=$this->table->fetchAll('id <= 17', "name", 2, 1);
     foreach ($rowset as $row){
       echo "
";
     echo $row->id;
     echo "
";
     echo $row->name;
     echo "
";
     echo $row->password;
     echo "
";
     echo $row->age;
     echo "
";
     echo $row->email;
     }
     /* Initialize action controller here */
    }
===   
===row 结构
      $row = $this->table->fetchRow('id = 17', "name");
     echo "
";
     echo $row->id;
     echo "
";
     echo $row->name;
     $row->name = "bbb";
     $row->save();
===  
5.5 另一种修改记录的方法
$row->age = 30;
$row->save();
阅读(400) | 评论(0) | 转发(0) |
0

上一篇:关于Controller

下一篇:关于Config

给主人留下些什么吧!~~