CodeIgniter Forums
[CI4] Toolbar DB Query And Event Duplication - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [CI4] Toolbar DB Query And Event Duplication (/showthread.php?tid=77765)

Pages: 1 2


[CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-15-2020

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.


RE: [CI4] Toolbar DB Query And Event Duplication - InsiteFX - 10-15-2020

Try renaming your function to getOpt()


RE: [CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-16-2020

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


RE: [CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-16-2020

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?


RE: [CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-16-2020

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.


RE: [CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-16-2020

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


RE: [CI4] Toolbar DB Query And Event Duplication - InsiteFX - 10-16-2020

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(); 



RE: [CI4] Toolbar DB Query And Event Duplication - Awlikhaleghi - 10-16-2020

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


RE: [CI4] Toolbar DB Query And Event Duplication - paulbalandan - 10-20-2020

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.


RE: [CI4] Toolbar DB Query And Event Duplication - paulbalandan - 10-24-2020

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