【问题标题】:How to use Zend Adapter to retrieve information from database如何使用 Zend Adapter 从数据库中检索信息
【发布时间】:2012-07-04 22:29:35
【问题描述】:

我正在尝试找出 Zend Adapter 的基本要素。教程和解释令人困惑,需要更多澄清。谁能给我简单的例子来完全理解如何使用 SQL 查询以及如何获得所需的 SQL 结果?

尤其是,我有兴趣学习如何获得

-- 列名

-- 表名

-- 获取所有条目

等等

谢谢,

【问题讨论】:

    标签: mysql database zend-framework adapter


    【解决方案1】:

    像许多其他人一样,您似乎在快速入门方面遇到困难,请尝试来自 Rob Allen 的教程,它帮助我入门。

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

    使用一个 DB 时最简单的方法是,在您的 application.ini 文件中至少添加以下行:

    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'
    ));
    

    List of supported Databases

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

    //fetchAll using Zend_Db_Adapter and plain SQL
    $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
    
    $result = $db->fetchAll($sql, 2);
    

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

    $tables = $db->listTables();
    

    或者您可以获得完整的表描述(包括列名),我在 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_TableZend_Db_Table_Row,尤其是Zend_Db_Select 类。

    我敦促您花一些时间弄清楚它们。

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

    //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 模型以及它们如何工作。

    希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多