Home  >  Article  >  php教程  >  用Zend_Db替代propel和doctring数据抽象层(一)

用Zend_Db替代propel和doctring数据抽象层(一)

PHP中文网
PHP中文网Original
2016-05-25 17:13:441235browse

<?php/*
 *  有人说 Yii 写一个博客很简单 一天就好了
 *  我想说 Zend 写 我只需要15分钟 就有wordpress的界面
 *  Zend不只是一个框架 更是一种思想 一种最优秀的框架
 *  和php领域最彪悍的代码实现  
 * *///建立一个 Zend_Db_Adapter 抽象实例 因为不能直接NEW 所以//必须先抽象  Db模块会自动调用Db/adapter类对象 //同样 这还是一个抽象类  require_once &#39;Zend/Db.php&#39;;require_once &#39;Zend/Db/Table.php&#39;;$params = array(
    &#39;host&#39; => &#39;127.0.0.1&#39;,
    &#39;username&#39;=>&#39;root&#39;,
    &#39;password&#39;=>&#39;sa&#39;,
    &#39;dbname&#39;=>&#39;test&#39;);$dbh = Zend_Db::factory(&#39;PDO_MYSQL&#39;,$params);Zend_Db_Table::setDefaultAdapter($dbh);// 为所有的Zend_Db_Table 对象设定默认的adapter// 你只需要一个Db对象和一个adapter对象// 这里是使用的no-mvc模式  如果是标准zend application// 连adapter都不需要  ZEND_CONSOLE已经为你完成了adapter的工作class Albums extends Zend_Db_Table{} #你可以什么都不写 直接使用Zend_Db_Table#的方法  crud...//接下来我们假设链接的数据库有一张叫做albums的音乐列表$albums = new Albums();echo "<h2>test get the data from test.albums from MySQL-5.5.m6<br /></h2>";//rs 返回的结果集是一个默认的Zend_Db_Table_Row 对象foreach($albums->fetchAll() as $row){
    echo "<p>".htmlspecialchars($row->id)." ".
        htmlspecialchars($row->artist). "&#39;s" . 
        htmlspecialchars($row->title). ".</p>\n";
    $row->save();}/**
1 张学友&#39;s她来听我的演唱会.

2 黄小琥&#39;s没那么简单.

3 久石让&#39;ssummer.

4 汪峰&#39;s春天里.

5 Paolo Nutine&#39;sSunny Side Up.

6 Andre Attack&#39;sHeligoland.

7 Beatles&#39;sImage.

8 Beatles&#39;sHey Jude.

9 王菲-《你王菲所以我王菲》&#39;s红豆,因为爱情.

10 约翰尼德普&#39;s伦敦达到.
*///现在我们修改一下 第十条数据 把 伦敦达到改为 the green fariy//修改行数据是一件很轻松的事情:只需要按照常规的方法修改类属性.然后调用save()方法 就将改变的结果保存到了数据表中. //有很多办法可以做到这一点  比如 重新声明一个//Zend_Db_Statement  不过我喜欢简单的方法$row = $albums->fetchRow(&#39;id = "10"&#39;);$row->title = &#39;The Green Fairy ,i like UK-London style&#39;;$row->save();/**
test get the data from test.albums from MySQL-5.5.m6

1 张学友&#39;s她来听我的演唱会.

2 黄小琥&#39;s没那么简单.

3 久石让&#39;ssummer.

4 汪峰&#39;s春天里.

5 Paolo Nutine&#39;sSunny Side Up.

6 Andre Attack&#39;sHeligoland.

7 Beatles&#39;sImage.

8 Beatles&#39;sHey Jude.

9 王菲-《你王菲所以我王菲》&#39;s红豆,因为爱情.

10 约翰尼德普&#39;sThe Green Fairy ,i like UK-London style.
 *
 * */$row  = $albums->find(&#39;3&#39;);$row[0]->title = "菊次郎的夏天"; //注意:row本身是一个Zend_Db_Table_RowSet集合 $row[0]->save(); #不过集合是一个数组 映射到单一元素还是一个row对象 有save()方法 //越来越喜欢简单的东西  比如VIM 比如PHP Python//只有一个需要注意的地方 你不能修改ID 这会在DB级别抛出一个异常/**
 *test get the data from test.albums from MySQL-5.5.m6

1 张学友&#39;s她来听我的演唱会.

2 黄小琥&#39;s没那么简单.

3 久石让&#39;s菊次郎的夏天.

4 汪峰&#39;s春天里.

5 Paolo Nutine&#39;sSunny Side Up.

6 Andre Attack&#39;sHeligoland.

7 Beatles&#39;sImage.

8 Beatles&#39;sHey Jude.

9 王菲-《你王菲所以我王菲》&#39;s红豆,因为爱情.

10 约翰尼德普&#39;sThe Green Fairy ,i like UK-London style
 *
 */


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn