简体   繁体   English

替换CodeIgniter查询构建器

[英]Replace CodeIgniter Query builder

I'm trying to make my own implementation of the Query builder in codeigniter. 我正在尝试在codeigniter中实现查询生成器的自己的实现。 Basicly I would like to add some function and all the provided function. 基本上,我想添加一些功能以及所有提供的功能。 Something like : 就像是 :

$this-db->from('myTable')
        ->where('id', 1)
        ->custom_where('name', 'customsValues')
        ->get()

There values are irrelevent here. 这里的值是irrelevent。 I've already built a class that extends en current CI_DB_query_builder but I have no idea where to set my class to be used as the primary query builder. 我已经建立了一个扩展当前CI_DB_query_builder的类,但是我不知道在哪里设置我的类以用作主要查询构建器。 I've try to look up similar problem on google but could not find anything. 我试图在Google上查找类似的问题,但找不到任何东西。

Any help would be appreciated. 任何帮助,将不胜感激。

I've found the answer to my question. 我找到了我问题的答案。 However, any other good answer is welcome since mine is not the prettiest. 但是,任何其他好的答案都值得欢迎,因为我的不是最漂亮的。

First let me walk you through what I was trying to do. 首先,让我引导您完成我尝试做的事情。
I wanted to use Tightenco's collect library to use collection rather than array. 我想使用Tightenco的collect库来使用collection而不是array。 This way I could use more intuitive chain array function: 这样,我可以使用更直观的链数组功能:
$this->db->from('...')->results()->map(function($items) { ... })->toArray();

Then, i wanted to have my own function, such as where_if . 然后,我想拥有自己的功能,例如where_if

I started by making my own query builder class that extended from CI_DB_query_builder . 我开始制作自己的查询构建器类,该类从CI_DB_query_builder扩展。 I have an example below: 我有一个例子如下:

<?php
require BASEPATH . 'database/DB_query_builder.php';


class CustomQueryBuilder extends CI_DB_query_builder {
    public function where_if($where, $value, $condition) : CustomQueryBuilder {
        if($condition) {
            $this->where($where, $value);
        }
        return $this;
    }

    public function results() : \Tightenco\Collect\Support\Collection {
        return \Tightenco\Collect\Support\Collection::make($this->get()->result_array());
    }
}

To link this class as the main Querybuilder, I had to change it in the file system/database/DB.php . 要将此类链接为主Querybuilder,我必须在文件system/database/DB.php
I changed the path of the require_once in line 171 : 我在第171行中更改了require_once的路径:

require_once(APPPATH.'libraries/QueryBuilder/CustomQueryBuilder.php');

I also changed the alias class on line 182 我还更改了182行的别名类

class CI_DB extends CustomQueryBuilder { }

Please note that this is on codeigniter v3.0.6, your line number may differ. 请注意,这是在codeigniter v3.0.6上的,您的行号可能有所不同。

Now, i needed to import some function so the autocomplete on PHPStorm would still point to my custom querybuilder, because once i used the function from , it returned a CI_DB_query_builder object. 现在,我需要导入一些函数,因此PHPStorm上的自动完成功能仍将指向我的自定义querybuilder,因为一旦使用了from的函数,它将返回CI_DB_query_builder对象。
Here is how I imported the function I used the most. 这是我最常使用的功能的导入方式。

/**
 * Nothing changed here, for autocompletion only
 * @param mixed $from
 * @return $this|CI_DB_query_builder
 */
public function from($from) {
    parent::from($from);
    return $this;
}

I really hope this helps people that are trying the same thing. 我真的希望这对尝试相同操作的人有所帮助。 If you got any feedback on this project, please let me know ! 如果您对此项目有任何反馈,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM