codeigniter - pyrocms拦截和修改所有的查询;延伸活动记录 [英] codeigniter - pyrocms intercept and modify all queries; extending active record

查看:181
本文介绍了codeigniter - pyrocms拦截和修改所有的查询;延伸活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来捕获所有的查询和发送到数据库修改DB_active_record.php之外之前对其进行修改?这将是理想的,虽然我不反对修改该文件,如果它是唯一的选择。

Is there a way to catch all queries and modify them before being sent to the db outside of modifying DB_active_record.php? This would be ideal, though I'm not averse to modifying this file if it's the only option.

我可以把这个东西从型号上,但会感觉更好,如果这东西做背景,以免留下空间可以用来遗忘的时间和地点,它的发生;它会是最好有ID在后台完成。

I could just call this stuff from the models, but would feel better if this was something that was done in the background so as not to leave room for forgetting when and where it's happening; it'd be best to have id done in the background.

我使用的库来查询数据进行加密。在lib,我检查某些键不进行加密,所以它不只是一个问题加密所有数据,但在每个查询一些数据。

I'm using a library to encrypt query data. In the lib, I'm checking for certain keys to not encrypt, so it's not just a matter of encrypting all data, but some data in each query.

例如,修改插件()像这样:

For example, modifying insert() like so:

function insert($table = '', $set = NULL)
{
    if ( ! is_null($set))
    {
        $this->CI =& get_instance();
        $this->CI->load->library('my_encrypt');
        $set = $this->CI->my_encrypt->encrypt_query($set);
        $this->set($set);
    }

正常工作的数据发送到分贝之前进行加密。然而,这并没有什么更新/套,也没有解密。

works correctly to encrypt the data before sending to the db. However, this does nothing for updates/sets nor decryption.

有没有办法建立一个中介或其他东西,作为一个穿针引线的模型和活动记录,我可以加密,并把它发送到AR或回模型前的相关数据进行解密的?

Is there a way to set up an intermediary something or other that acts as a go-between between models and active record where I could encrypt and decrypt the relevant data before sending it on to AR or back to the model?

感谢。

所以,按照下面的指导,我能得到这个工作。我使用pyroCMS(版本1.3.2社区; CI版本2.0.2)中已做了一些跑腿的。

So following the guide below, I was able to get this working. I'm using pyroCMS (ver. 1.3.2 community; CI version 2.0.2) which had already done some of the legwork.

继承人就是我所做的。

Heres what I did.

系统/ CMS /核心/ MY_Loader.php 扩展 MX_loader 延伸 CI_Loader ;所以 MX_Loader 是我们需要修改。

system/cms/core/MY_Loader.php extends MX_loader which extends CI_Loader; so MX_Loader is what we need to modify.

系统/ CMS /库/ MX / Loader.php

改变公众函数库()为:

change public function database() to:

public function database($params = '', $return = FALSE, $active_record = NULL) {
    log_message('debug', 'db loader from MX/Loader');

    // Grab the super object
    $CI =& get_instance();

    // Do we even need to load the database class?
    if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db)) {
        return FALSE;
    }

    require_once(BASEPATH.'database/DB'.EXT);

    // Load the DB class
    $db =& DB($params, $active_record);

    //get the driver name, set its path and load er up
    $my_driver = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
    $my_driver_file = APPPATH.'core/'.$my_driver.EXT;

    if (file_exists($my_driver_file)) {
        require_once($my_driver_file);
        $db = new $my_driver(get_object_vars($db));
    }

    if ($return === TRUE) {
        return $db;
    }

    CI::$APP->db = DB($params, $active_record);
    return CI::$APP->db;
}

这里最重要的区别,从这篇文章中的接受的答案链接是数据库的设置瓦尔因为火法做它不同于常规的CI。

The important difference here from the article linked in the accepted answer is the setting of the db vars as pyro does it differently from regular CI.

然后在系统/ CMS /芯添加自定义的驱动程序文件 MY_DB_mysql_driver.php 。这将被加载,这些方法将在所有型号。此外,在这里使用相同的名称 DB_active_rec.php方法将precedence。例如加入插入方法MY_DB_mysql_driver.php将覆盖插入方法 DB_active_rec.php

Then in system/cms/core add the customized driver file MY_DB_mysql_driver.php. This will then be loaded and these methods will be available in all models. Also, methods in here using the same name as in DB_active_rec.php will take precedence. e.g. adding an insert method in MY_DB_mysql_driver.php will override the insert method in DB_active_rec.php.

推荐答案

在默认情况下,你不能扩展活动记录类。但是,可以延长活动记录类的扩展类加载数据库调用,以允许它:

By default you cannot extend the active record class. You can however, extend the active record class after extending the loader class database call to allow it:

说明和放大器; code在这里:的http:// www.simonemms.com/$c$c/extending-the-$c$cigniter-database-class/

Instructions & code here: http://www.simonemms.com/code/extending-the-codeigniter-database-class/

这篇关于codeigniter - pyrocms拦截和修改所有的查询;延伸活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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