在 Zend Framework 中确定 MySQL 查询类型 [英] Determine MySQL query type in Zend Framework

查看:25
本文介绍了在 Zend Framework 中确定 MySQL 查询类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何确定查询类型,即在通过 MySQL 执行之前选择、更新、删除或插入.

Can anyone tell me how to determine query type i.e. select, update, delete or insert before it is executed over MySQL.

我坚信 Zend Framework 在内部可能正在使用 mysql*_query 函数来执行它们.

I strongly believe internally Zend Framework might be using mysql*_query function to execute them.

我需要一个在执行前返回查询类型的集中函数.

I need a centralized function which will return the query type before the execution.

我为每个数据库表使用三个文件

I am using three files for every database table

我给你举个例子.

假设我想为类别表创建模型.

Say I want to create model for categories table.

所以我将创建以下文件

DbTable/Categories.php

class Application_Model_DbTable_Categories extends Zend_Db_Table_Abstract {
    protected $_name = 'categories';
    protected $_dependentTables = array('Application_Model_DbTable_Videos');
}

Categories.php

class Application_Model_Categories extends Application_Model_CommonGetterSetter {
    protected $_type = array('id' => 'int', 'name' => 'string', 'slug' => 'string', 'status' => 'int');

    public function __construct(array $options = null) {
        parent::__construct($options, __CLASS__);
    }
}

CategoriesMapper.php

class Application_Model_CategoriesMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {

        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;
        return $this;
    }



    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Categories');
        }
        return $this->_dbTable;
    }



    public function save(Application_Model_Categories $categories) {
        $data = array(
                'name'      => $categories->name,
                'slug'      => $categories->slug,
                'status'    => $categories->status,
        );

        if (@$categories->id <= 0) {
            return $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $categories->id));
            return $categories->id;
        }
    }

CommonGetterSetter.php

class Application_Model_CommonGetterSetter {

    protected $_data = array();
    private $_class_name;

    public function __construct(array $options = null, $class_name = null) {
        if (is_array($options)) {
            foreach ($options as $key => $value) {
                $this->$key = $value;
            }
            $this->_class_name = $class_name;
        }
    }

    public function __set($name, $value) {
        if (!array_key_exists($name, $this->_type)) {
            throw new Exception("Invalid {$this->_class_name} property". $name);
        }
        else {
            settype($value, $this->_type[$name]);
            $this->_data[$name] = $value;
        }
    }

    public function __get($name) {
        if (!array_key_exists($name, $this->_type)) {
            throw new Exception("Invalid {$this->_class_name} property". $name);
        }
        else {
            return $this->_data[$name];
        }
    }
}

所以我想找出执行了哪个查询,我应该写在哪里以及写什么?

So I want to find out which query was executed , where and what should i write?

谢谢你.

我知道代码有点长,但这是为了提供一个完整的想法.

I know the code is bit long but that was to give a complete idea.

推荐答案

我坚信 Zend Framework 内部可能正在使用 mysql*_query 函数来执行它们.

I strongly believe internally Zend Framework might be using mysql*_query function to execute them.

ZF 的数据库类不支持陈旧的 mysql 扩展.它有用于 PDO 和 mysqli 的适配器.您会知道您使用的是哪一个,因为您必须在代码或配置中明确指定.

ZF's Database class does not have support for the decrepit mysql extension. It has adapters for PDO and mysqli. You'll know which of these you are using because you have to expressly specify one in your code or configuration.

所以我想找出执行了哪个查询

So I want to find out which query was executed

我认为重新思考您的设计会更好地为您服务.目前还不清楚这里发生了什么,也不清楚为什么您认为您需要知道查询类型,也不清楚您希望在哪里需要知道查询类型.

I think that you're going to be better served by rethinking your design. It's not entirely clear what is going on here, nor is it clear why you think you need to know the query type, nor is it clear where you expect to need to know the query type.

请查看 Zend_Db_Table 文档,因为它看起来像您可能在途中的某个地方错过了重点.

Please review the Zend_Db_Table documentation, as it looks like you might have missed the point somewhere along the way.

具体来说,Zend_Db_Table 已经提供了插入、删除和更新的方法.如果你需要改变适当方法的行为,你应该通过 只需在您的类中覆盖它.

In specific, there are already methods provided by Zend_Db_Table for inserts, deletes and updates. If you need to change the behavior of the appropriate method, you should do so by simply overriding it in your class.

此外,您似乎正在尝试构建某种智能列类型定义系统.您不需要这样做,因为 Zend_Db_Table 已经向您提供了这些信息,并且 甚至允许您对其进行硬编码,如果您不希望它自动确定此信息.

Further, it looks like you're trying to build some sort of smart column type definition system. You don't need to do that, as again, Zend_Db_Table already provides this information to you, and even lets you hard-code it if you'd rather it not determine this information automatically.

ZF 是一个复杂的野兽.如果您要使用它,我建议您完全了解它的所有功能.构建非标准的冗余架构只会让您以后的事情变得复杂.

ZF is a complicated beast. If you're going to use it, I suggest that you fully understand everything it can do. Building non-standard, redundant architecture is only going to complicate things for you later on.

这篇关于在 Zend Framework 中确定 MySQL 查询类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆