Welcome Guest, Not a member yet? Register   Sign In
[CI4] Toolbar DB Query And Event Duplication
#1

(This post was last modified: 10-15-2020, 04:35 PM by Awlikhaleghi.)

Hi everybody.
I'm relatively new with the CI4, I've been learning it as I go and to be honest loving the changes so far.

My DB Queries are happening twice, no matter where they're executed from.
[Image: screenshot-from-2020-10-16-02-34-51.png]

these queries are being executed from several different files.
the function which runs the query is a helper as follows:
PHP Code:
function getOption(string $key)
{
    return model("OptionsModel",TRUE)->getOption($key);


and the `getOption()` function inside OptionsModel is as:
PHP Code:
public function getOption(string $key){
        if(isset(self::$loadedOptions[$key]))
            return self::$loadedOptions[$key];

        $row $this->select('option_value')
            ->where("option_key",$key)
            ->get()
        ->getRow();

        if(!$row)
            return false;

        self::$loadedOptions[$key] = $row->option_value;

        return $row->option_value;
    
Even though these functions themselves are not the problem, I wanted to explain anyways.
no matter how the query is being executed, the CI4 toolbar is always recording two of each SELECT query.
my inserts are fine and are happening only once.

and also my Events are happening twice as well.
Meaning having the following code in a view file:

PHP Code:
\CodeIgniter\Events\Events::trigger('TEST'); 
and having the following code in the Events.php
PHP Code:
Events::on('TEST', function () {
 echo 
"ON TEST EXECUTION.";
}); 
would produce "ON TEST EXECUTION.ON TEST EXECUTION." upon triggering TEST in view file.

if anyone can point me to the right direction so I can troubleshoot this it'd be greatly appreacated.
Reply
#2

Try renaming your function to getOpt()
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-15-2020, 11:04 PM)InsiteFX Wrote: Try renaming your function to getOpt()
didn't work.
as I mentioned it doesn't matter where the query is executing from whether its a model or a direct query in a view.
Reply
#4

I'm really hanging here, I need to fix this fast but so far I can't figure out why these duplications are happening.
Any other ideas out there?
Reply
#5

After quite some investigations here are my findings regarding Event duplications.
It happened to be Events.php file being included twice by CI Event Object.

PHP Code:
public static function initialize()
    {
        
// Don't overwrite anything....
        
if (static::$initialized)
        {
            return;
        }

        
$config config('Modules');

        
$files = [APPPATH 'Config/Events.php'];

        if (
$config->shouldDiscover('events'))
        {
            
$locator Services::locator();
            
$files   $locator->search('Config/Events.php');
        }

        static::
$files $files;


        foreach (static::
$files as $file)
        {
            if (
is_file($file))
            {
                include 
$file;
            }
        }

        static::
$initialized true;
    } 
this piece of code is finding two Events.php with two different paths (even though they're one file).
Code:
array(2) {
  [0]=>
  string(74) ".../public_html/Deli/Config/Events.php"
  [1]=>
  string(22) "Deli/Config/Events.php"
}

Although both files are in the same path, CI4 has decided that it is required for the files to be included.

Now I have slightly modified the function to:
Code:
if(false) //if ($config->shouldDiscover('events'))
{
$locator = Services::locator();
$files  = $locator->search('Config/Events.php');
}
this will disable auto discovery of Events.php and only includes the Events.php file defined previously.

Now I'm back at db queries, still looking for answers; I will post the solution if I found one.
Reply
#6

WOW!
Actually that one simple modification fixed both events and db queries, I guess they were linked somehow.
I'm done, thanks anyways.
Reply
#7

PHP Code:
// Change this to the below.
$row $this->select('option_value')
           ->where("option_key",$key)
           ->get()
       ->getRow();

// Change to this and see if it makes a deference.
$query $this->select('option_value')
           ->where("option_key",$key)
           ->get();

$row $query->getRow(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

That would not have helped either, I've posted my fix and it's being moderated.
thanks for the effort anyways!
Reply
#9

You are modifying the core Events class, which on subsequent CI updates your changes will unfortunately be overridden. I think you hit the jackpot there but instead of modifying the check on events discovery, you should check instead if the file to be included is not the same app file in app/Config/Events.php . I think you should open an issue ticket in the Github repo to properly address this.
Reply
#10

Good news!! This has been fixed in latest develop branch.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB