Magento开发小技巧

Magento多层继承,往往让初学者感觉到很头疼,除了要掌握一些常用的设计模式,尽量针对接口进行编程,有时候 通过记录sql日志是一个不错的分析工具,

比如  Model->save();你知道他到底是执行什么sql语句呢。

在lib\Varien\Db\Adapter\Pdo/Mysql.php有一个query方法,可以打印输出,我想这种方式在很多更复杂的系统,也是一样的吧。贴出代码如下
 
    public function query($sql, $bind = array())
    {
        // connect to the database if needed
        $this->_connect();
        Mage::log($bind,null,'sql.log',true);
        Mage::log($sql,null,'sql.log',true);
        // is the $sql a Zend_Db_Select object?
        if ($sql instanceof Zend_Db_Select) {
            if (empty($bind)) {
                $bind = $sql->getBind();
            }

            $sql = $sql->assemble();
        }

        // make sure $bind to an array;
        // don't use (array) typecasting because
        // because $bind may be a Zend_Db_Expr object
        if (!is_array($bind)) {
            $bind = array($bind);
        }

        // prepare and execute the statement with profiling
        $stmt = $this->prepare($sql);
        $stmt->execute($bind);

        // return the results embedded in the prepared statement object
        $stmt->setFetchMode($this->_fetchMode);
        return $stmt;
    }

你可能感兴趣的:(agent)