0

How can I properly make my own implementation of a query builder method in CodeIgniter.

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

There values are irrelevent here. 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.

1 Answer 1

1

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. 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.

I started by making my own query builder class that extended from 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.
I changed the path of the require_once in line 171 :

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

I also changed the alias class on line 182

class CI_DB extends CustomQueryBuilder { }

Please note that this is on codeigniter v3.0.6, your line number may differ.

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.
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 !

Sign up to request clarification or add additional context in comments.

5 Comments

change it in the file system/database/DB.php in CI typically you can place a class replacing CI with MY in the app/core folder to override it, so instead of CustomQueryBuilder (or the original CI_DB_query_builder) your class would be MY_DB_query_builder etc. And then CI will use the MY version instead. I use a heavily modified CI2 install, doing it this way...
Sure, it's important because if you edit the system files, the next time you update your CI install you run the very real risk of losing that change. Which may or may not be obvious from light testing. In other words you could update CI sometime in the future and then months later find a part of you site broken and realize you lost those code changes, something to be avoided if possible as when that happens, not only do you lose your code change (so you have no example of what you did), you may not remember exactly were or what it was.
Unfortunaly, Because of the way the querybuilder instantiated, it is not possible to use @ArtisticPhoenix Solutions.
Explaining what you were trying to do should be in the question body, not your answer.
I agree. Looking back, the context could have been useful to maybe propose another solution that doesn't revolve around editing system files. Clearly my solution here might not be the best. Thank you for your feedback, I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.