繁体   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