簡體   English   中英

什么時候應該使用doctrine ORM和zend-db-table?

[英]When should use doctrine ORM and when zend-db-table?

在項目規模,學說與zend-db-table速度和性能方面,何時應該在Zend項目中使用doctrine,以及什么時候使用zend-db-table?

任何ORM框架都可以提高開發效率,而不是運行時效率。 在這方面,Doctrine與Zend_Db_Table沒有什么不同。

如果您在Doctrine和Zend_Db_Table之間進行選擇,請根據使編寫代碼更容易或更快的功能進行選擇。

在一般情況下,沒有ORM框架可以自動更快地進行數據庫查詢。 如果需要高性能數據庫查詢,則應學習編寫SQL查詢代碼,並設計模式和索引以根據需要運行的查詢來支持性能。

使用您最滿意的任何東西,將使您最有效。 您和您的開發人員可能是最昂貴的資源,如果需要,購買額外硬件可能比您不必擔心未來可能的性能考慮因素更便宜。

當然,您仍然需要執行基本的數據庫優化,例如創建合理的索引等。但我覺得那些“優化”不言而喻。

如果你有很多SQL查詢,缺乏知識(對緩存或查詢優化一無所知等)並且缺乏時間使用Zend_DB它更快更容易理解,對你來說已經足夠了 - 那個人缺乏知識和時間,希望盡可能快。

但如果你想要一個殺手ORM主義獲勝。 但它需要更多的時間和精力才能有效地使用。

如果你想成為一名數據庫殺手,我們就會脫離主題。 他們有區別。 它不一定基於您使用的工具。 (一些DB殺手可能會使用PDO本身和他們自己的模型,誰知道?)

Doctrine可以幫助您定義更好的業務邏輯和ORM,但就內存和CPU而言,它是絕對的性能殺手。 Zend表網關比Doctrine快5倍。 並且您可以擴展Zend表網關以刪除一些數據類型解析器,這將使您的性能提高5倍,同時保留了簡單的ORM的優點。 這是我的FastTablegateway類:

<?php
namespace Application\Libraries;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;

class FastTablegateway extends TableGateway{

    protected $mysqli_adapter = null;

    /**
     * Constructor
     *
     * @param string $table
     * @param AdapterInterface $adapter
     * @param Feature\AbstractFeature|Feature\FeatureSet|Feature\AbstractFeature[] $features
     * @param ResultSetInterface $resultSetPrototype
     * @param Sql $sql
     * @throws Exception\InvalidArgumentException
     */
    public function __construct($table, AdapterInterface $adapter, $features = null, ResultSetInterface $resultSetPrototype = null, Sql $sql = null, $mysqli = null)
    {
        $this->mysqli_adapter = $mysqli;
        parent::__construct($table, $adapter, $features, $resultSetPrototype, $sql);
    }


    protected function executeSelect(Select $select)
    {
        $time = time();
        $selectState = $select->getRawState();
        if ($selectState['table'] != $this->table) {
            throw new \Exception\RuntimeException('The table name of the provided select object must match that of the table');
        }

        if ($selectState['columns'] == array(Select::SQL_STAR)
        && $this->columns !== array()) {
            $select->columns($this->columns);
        }

        //apply preSelect features
        $this->featureSet->apply('preSelect', array($select));

        if(!$this->mysqli_adapter){
            // prepare and execute
            $statement = $this->sql->prepareStatementForSqlObject($select);
            $result = $statement->execute();
        }else{
            $q = $this->sql->getSqlStringForSqlObject($select);
            //var_dump($q);
            $result = $this->mysqli_adapter->query($q);
            $result = is_object($result) ? $result->fetch_all(MYSQLI_ASSOC) : [] ;
        }

        // build result set
        $resultSet = clone $this->resultSetPrototype;
        //var_dump(is_object($result) ? $result->num_rows : 'A');
        $resultSet->initialize($result);

        // apply postSelect features
        //$this->featureSet->apply('postSelect', array($statement, $result, $resultSet));

        return $resultSet;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM