Welcome Guest, Not a member yet? Register   Sign In
Builder *and* beforeInert beforeUpdate
#1

I sorted out (with help of a post here) that the Builder functionality and the database model's use of beforeInsert and beforeUpdate don't work together.

I would like to extend the Model and write code for the builder (BaseBuilder) to insert the beforeInsert and beforeUpdate (might as well do the afters too, but whatever).
(side note: Would have been great if the builder functionality also worked with the Model settings, since, ya know, it seems like the protections from the builder and the functionality from the model together makes a good database interface, instead of picking one or the other and having to write the code to do what the other half would have done for you. Real missed opportunity here...)


I'm aware I need to put *something* in the autoloading section and all.

What I would like help with (unless someone else also found the Builder function lacking and has done this, then I need the webpage for their code) is what do I need to do to extend the Model and BaseBuilder core files (as in, what do I write and put where so I can just write the code and it will all work as expected without mucking up the main functions)

For the Model, I need to replace the builder function, so I will extend the class (https://codeigniter.com/user_guide/exten...re-classes) - where do I put this new Model class file in the App folder? How do I setup the autoloading files in the config to correctly see that file.

For the BaseBuilder, I need to update the update and insert functions, again extending the class. Do I just do the same thing as the Model (create the new file in the same place and do the same thing with the autoloading files)?

Thanks. Wish the docs where more helpful.
Reply
#2

If you want to extend Builder, it seems you need to extend all database classes.

a simple way to extend the Database classes
https://forum.codeigniter.com/thread-782...#pid383813
Reply
#3

I'm not looking to extend the database functionality, I'm looking to be able to use the beforeInsert from the database model while using the builder functionality.  I believe I want to extend the BaseBuilder class in the System/Database folder.

So far, this isn't working:

PHP Code:
<?php

namespace App\Libraries;

use 
CodeIgniter\Database;

class 
BaseBuilder extends BaseBuilder {
    public function insert(array $set nullbool $escape null)
    {
        log_message("error","IN EXTENDED BASE BUILDER");
        if ($set !== null)
        {
            $this->set($set''$escape);
        }

        if ($this->validateInsert() === false)
        {
            return false;
        }

        $sql $this->_insert(
            $this->db->protectIdentifiers(
                $this->QBFrom[0], true$escapefalse
            
), array_keys($this->QBSet), array_values($this->QBSet)
        );

        if (! $this->testMode)
        {
            $this->resetWrite();

            $result $this->db->query($sql$this->bindsfalse);

            // Clear our binds so we don't eat up memory
            $this->binds = [];

            return $result;
        }

        return false;
    }


I'm not seeing my log message in what I believe should be an overwritten class function (according to Extending Core Classes in the docs)
Reply
#4

Extending Core Classes https://codeigniter.com/user_guide/exten...re-classes
is based on Replacing Core Classes https://codeigniter.com/user_guide/exten...re-classes

That is, you need to add the service in app/Config/Services.php to load your class.

But there is no service for database classes.
So if you want to extend BaseBuilder, you need to extend the Database classes.
Reply
#5

(06-23-2022, 04:33 AM)kenjis Wrote: Extending Core Classes https://codeigniter.com/user_guide/exten...re-classes
is based on Replacing Core Classes https://codeigniter.com/user_guide/exten...re-classes

That is, you need to add the service in app/Config/Services.php to load your class.

But there is no service for database classes.
So if you want to extend BaseBuilder, you need to extend the Database classes.

Ok, thanks for clarifying that. I've found this to be the case as I've worked on it as well. Just gonna re-write the models to not use the builder instead for updates/inserts. Time constraints and all for the project at hand.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB