如何使用Zend的适配器从数据库中获取信息(How to use Zend Adapter to r

2019-10-16 22:57发布

我想弄清楚的Zend适配器的要领。 教程和解释是混乱的,需要更多的澄清。 能有人给我简单的例子,以充分了解如何使用SQL查询的工作,以及如何获得所需的SQL结果?

特别是,我想学习如何获得

- 列名

- 表名

- 使用fetchall项

等等。

谢谢,

Answer 1:

像很多其他人的你似乎是具有快速启动困难,尝试从教程罗布·艾伦它帮助我上手。

你如何连接到你的表有多种选择,与Zend_Db的混乱往往从这里开始。

使用一个数据库时,你最简单的方法application.ini文件在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'
));

支持的数据库列表

在应用程序中使用这个可以很简单的:

//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_Table类 , 一个Zend_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)
    }
}

罗布·艾伦的Zf的教程将介绍如何车型DBTABLE都设置以及它们如何工作。

希望这可以帮助...



文章来源: How to use Zend Adapter to retrieve information from database