如何使用Zend Adapter从数据库检索信息 [英] How to use Zend Adapter to retrieve information from database

查看:78
本文介绍了如何使用Zend Adapter从数据库检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清Zend Adapter的基本要素。这些教程和说明令人困惑,需要更多说明。有人可以给我简单的例子来完全理解如何使用SQL查询以及如何获得所需的SQL结果吗?

I'm trying to figure out the essentials of Zend Adapter. The tutorials and explanations are confusing and needs more clarification. Can someone give me simple examples to fully understand how to work with SQL queries and how to get the desired SQL results?

尤其是我对学习如何获取感兴趣。

especially, I'm interested in learning how to get

-列名

-表名

-fetchAll条目

-- fetchAll entries

等。

谢谢,

推荐答案

就像其他许多您似乎在快速入门方面遇到困难的人一样,请尝试从罗伯·艾伦它帮助我入门。

Like a lot of other people you seem to be having difficulty with the quickstart, try the tutorial from Rob Allen it helped me get started.

您有多种选择关于如何连接到表的问题,与Zend_Db的混淆通常从这里开始。

You have multiple choices on how to connect to your table, the confusion with Zend_Db often begins here.

application.ini中使用一个数据库的最简单方法文件至少添加以下行:

The easiest way when using one DB, in your application.ini file add these lines at a minmum:

resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user_name"
resources.db.params.password = "password"
resources.db.params.dbname = "db_name"

或者,您可以使用 Zend_Db_Adapter

//using a normal constructor
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

//using factory
$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

受支持的数据库列表

在您的应用程序中使用它可以很简单:

Using this in your application can be as simple as:

//fetchAll using Zend_Db_Adapter and plain SQL
$sql = 'SELECT * FROM bugs WHERE bug_id = ?';

$result = $db->fetchAll($sql, 2);

您可以列出数据库中的表:

you can list the tables in a database:

$tables = $db->listTables();

或者您可以获得完整的表描述(包括列名),我在Zend_Db_Adapter_Abstract中的函数:

or you can get a full table description (including column names), I included the comment block from the function in Zend_Db_Adapter_Abstract:

 /**
     * Returns the column descriptions for a table.
     *
     * The return value is an associative array keyed by the column name,
     * as returned by the RDBMS.
     *
     * The value of each array element is an associative array
     * with the following keys:
     *
     * SCHEMA_NAME => string; name of database or schema
     * TABLE_NAME  => string;
     * COLUMN_NAME => string; column name
     * COLUMN_POSITION => number; ordinal position of column in table
     * DATA_TYPE   => string; SQL datatype name of column
     * DEFAULT     => string; default expression of column, null if none
     * NULLABLE    => boolean; true if column can have nulls
     * LENGTH      => number; length of CHAR/VARCHAR
     * SCALE       => number; scale of NUMERIC/DECIMAL
     * PRECISION   => number; precision of NUMERIC/DECIMAL
     * UNSIGNED    => boolean; unsigned property of an integer type
     * PRIMARY     => boolean; true if column is part of the primary key
     * PRIMARY_POSITION => integer; position of column in primary key
     *
     * @param string $tableName
     * @param string $schemaName OPTIONAL
     * @return array
     */
$describTable = $db->describeTable('myTable');

此信息应该可以帮助您入门,但是我发现Zend_Db的真正强大之处在于 Zend_Db_Table Zend_Db_Table_Row 尤其是 Zend_Db_Select 类。

This info should get you started, however I find that a lot of the real power of Zend_Db resides in the Zend_Db_Table, Zend_Db_Table_Row and especially the Zend_Db_Select classes.

我敦促您花些时间和精力他们出来。

I urge you to take some time and figure them out.

作为Zend_Db_Table和Zend_Db_select的预期示例(当不使用更高级的映射器和域对象时,希望它们会在以后出现):

As an example of what you might expect from Zend_Db_Table and Zend_Db_select (when not using more advanced mappers and domain objects, hopefully those will come later):

//When using DbTable models that extend Zend_Db_Table_Abstract the model already 
//knows the name of the table and has full access to the Db adapter, allowing your code to
//be very brief and descriptive.
class Application_Model_DbTable_Weekend extends Zend_Db_Table_Abstract
{
    //name of table, required if classname is not the same as the table name
    protected $_name = 'weekend';
    //primary key column of table, a good idea especially if primary key is not 'id'
    protected $_primary = 'weekendid';

    public function getWeekend($weekendId) {
        //create select object
        $select = $this->select();
        $select->where('weekendid = ?', $weekendId);//placeholder syntax

        $result = $this->fetchRow($select);       
        if (!$result) {
            throw new Exception('Could not find weekend ID ' . $weekendId);
        }
        return $result;//returns a single row object
    }
    public function fetchAllWeekend() {

        $select = $this->select();

        $result = $this->fetchAll($select);

        return $result; //returns array of row objects (rowset object)
    }
}

Rob Allen的Zf教程将解释DbTable模型的设置方式及其工作方式。

Rob Allen's Zf tutorial will explain how DbTable models are setup and how they Work.

希望这会有所帮助...

hope this helps...

这篇关于如何使用Zend Adapter从数据库检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆