zend_db 独立
我想使用 zend_db 独立的,因为 zend 框架对于我的项目来说太多了,但我是新来的, 这样做是否正确:
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => '设置名称 UTF8;');
<前><代码> $params = 数组( '主机' => '本地主机', '用户名' => 'ss_fraat', '密码' => '乔斯10', '数据库名称' => '_a2hDB', 'driver_options' =>; $pdo参数 ); 尝试 { $db = Zend_Db::factory('PDO_MYSQL', $params); //设置默认适配器 Zend_Db_Table_Abstract::setDefaultAdapter($db); } catch (异常$e) { 退出($e->getMessage()); } //将Db保存到注册表中以供以后使用 Zend_Registry::set('dbAdapter', $db);
然后在任何课程中执行此操作:
$db = Zend_Registry::get('db');
/** quote to avoid sql injection */
$date = $db->quote('1980-01-01');
$sql = 'SELECT * FROM product WHERE name = ' . $date;
$result = $db->query($sql);
$db->query(); //run a query
我真的需要这样做,
Zend_Db_Table_Abstract::setDefaultAdapter($db);
我从网站获取此代码, 如果我没有使用完整的 zend 框架,是否有必要使用 Zend_Db_Table_Abstract, 或者最好使用这个:
$db = Zend_Db::factory( ...options... ); $select = new Zend_Db_Select($db);
我想要的是在我的 bootstrap php 页面中设置一个 pdo/mysql 连接,并且能够在任何类中获取该数据库实例,而无需启动新的连接来执行查询,但我不知道如何使用 Zend_Db_Table_Abstract 或 Zend_Db_Select 使用注册表 Zend_Registry::set('dbAdapter', $db) 与否 多谢
i want to use zend_db standalone cos zend framework is too much for my project but i'm new with it,
is it correct to do this:
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES
UTF8;');$params = array( 'host' => 'localhost', 'username' => 'ss_fraat', 'password' => 'jos10', 'dbname' => '_a2hDB', 'driver_options' => $pdoParams ); try { $db = Zend_Db::factory('PDO_MYSQL', $params); //set default adapter Zend_Db_Table_Abstract::setDefaultAdapter($db); } catch (Exception $e) { exit($e->getMessage()); } //save Db in registry for later use Zend_Registry::set('dbAdapter', $db);
then in any class do this:
$db = Zend_Registry::get('db');
/** quote to avoid sql injection */
$date = $db->quote('1980-01-01');
$sql = 'SELECT * FROM product WHERE name = ' . $date;
$result = $db->query($sql);
$db->query(); //run a query
i really need to do this
Zend_Db_Table_Abstract::setDefaultAdapter($db);
i get this code from a website,
is it necessary to use Zend_Db_Table_Abstract if i'm not using the full zend framework,
or it is better for example to use this:
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($db);
what i want is to setup a pdo/mysql connexion in my bootstrap php page and be able to get that db instance in any class without starting a new connexion to execute queries but i'm not sure how to do that use Zend_Db_Table_Abstract or Zend_Db_Select use the registry Zend_Registry::set('dbAdapter', $db) or not
thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Zend_Db_Table_Abstract 的目的是让您可以基于表数据网关设计模式创建您自己的模型类。该模式的想法是,您有一个类封装了与表交互所需的所有 sql。因此,假设您将为每个表创建扩展 Zend_Db_Table_Abstract 的模型类。如果您打算这样做,那么您将需要在 setup/bootstrap 中调用 Zend_Db_Table_Abstract::setDefaultAdapter($db) 。 ZF 的最新版本提供了一种快速获取基本功能的方法,而无需通过实例化 Zend_Db_Table 来创建自定义类定义:
总而言之,这一切都与框架的 MVC 部分无关,尽管有些人选择使用 Zend_db 作为数据库连接和模型的基础,而不是使用 Doctrine 或 Propel 等功能更齐全的 ORM。
您提供的其他代码只是说明您也不需要使用 Zend_Db_Table_Abstract —— 您可以简单地设置 Zend_Db_Adapter 的实例并使用该实例来调用 query() 或其其他方法。
The purpose of Zend_Db_Table_Abstract is so you can create your own model classes based around the Table Data Gateway design pattern. The idea of that pattern is that you have a class that encapsulates all the sql you would need for interfacing with a table. So the assumption is that you will be creating model classes that extend Zend_Db_Table_Abstract for each table. If you are going to do that, then you will want to call Zend_Db_Table_Abstract::setDefaultAdapter($db) in your setup/bootstrap. Recent versions of ZF provide as an alternative a quick way of getting basic functionality without having to create a custom class definition by just instantiating Zend_Db_Table:
In summary, none of this particularly has to do with the MVC part of the framework, although some people choose to use Zend_db as the basis for db connections and models, instead of using a more fully featured ORM like Doctrine or Propel.
The other code you provided simply illustrates that you do not need to use Zend_Db_Table_Abstract either -- you can simply setup an instance of a Zend_Db_Adapter and use that instance to call query() or its other methods.