• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
undefined model method does not rise error

#1
Hello,
I've noticed that when you call a model->method that hasn't been defined, CI doesn't display any error (see the example below)

PHP Code:
$model model('MyModel');
$model->undefinedMethod(); 

In my case, this is an undesired behavior, can be very tricky to debug
I've solved the problem temporarily by commenting the definition of system/Model::__call()

Is there a config parameter to control it?
What is your opinion?

EDIT: here's a workaround that keeps the model magics but rises error if you call an undefined method, hope this helps

hack:
PHP Code:
        elseif (method_exists($this$name)) {
            
$result $this->$name(...$params);
        }

        if (! 
$result) {
            
$className get_class($this);
            throw new \
Exception("Call to undefined method $className::$name");
        } 

complete method:
PHP Code:
    public function __call(string $name, array $params)
    {
        
$result null;

        if (
method_exists($this->db$name))
        {
            
$result $this->db->$name(...$params);
        }
        elseif (
method_exists($builder $this->builder(), $name))
        {
            
$result $builder->$name(...$params);
        }
        elseif (
method_exists($this$name)) {
            
$result $this->$name(...$params);
        }


        if (! 
$result) {
            
$className get_class($this);
            throw new \
Exception("Call to undefined method $className::$name");
        }

        
// Don't return the builder object unless specifically requested
        //, since that will interrupt the usability flow
        // and break intermingling of model and builder methods.
        
if ($name !== 'builder' && empty($result))
        {
            return 
$result;
        }
        if (
$name !== 'builder' && ! $result instanceof BaseBuilder)
        {
            return 
$result;
        }

        return 
$this;
    } 
Reply

#2
I hope this is a bug. Because if this is intentional, like you say it will be hard to test and debug! I will try to reproduce this...
Test your translation files with Translation Tester
Reply

#3
He's correct I just tried it here and it gave me no error also using the DebugToolBar.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
Glad to see I'm not alone, so it seems it's not a desired behavior
I've tested a workaround and added the code to the first post
Though I'm not sure it could be a bugfix 'cause I don't know the system architecture that well
Reply

#5
To learn the CodeIgniter architecture just open and follow the code in index.php and .\system\Codeigniter.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
@carlorfeo,

I think this is is a major BUG and dread to think of the other repercussions!!!

> Is there a config parameter to control it?

Code:
# file: system/config/Services.php

public static function exceptions
(
  \Config\Exceptions $config = null,
  IncomingRequest $request = null,
  Response $response = null,
  bool $getShared = true
)
{
# John - KLUDGE UNTIL A FIX IS AVAILABLE        
# if(CI_DEBUG):    
#  OR     
if( 'development'===CI_ENVIRONMENT ):
  # USE STANDARD PHP ERRORS
else:
  if ($getShared)
  {
    return static::getSharedInstance('exceptions', $config, $request, $response);
  }

  if (empty($config))
  {
    $config = new \Config\Exceptions();
  }

  if (empty($request))
  {
    $request = static::request();
  }

  if (empty($response))
  {
    $response = static::response();
  }

  return (new Exceptions($config, $request, $response));
endif; # John - KLUDGE UNTIL A FIX IS AVAILABLE        
}

Output:
Quote:Fatal error: Uncaught Error: Call to a member function initialize() on null in /var/www/ci4-strict.tk/system/CodeIgniter.php:190
Stack trace:
#0 /var/www/ci4-strict.tk/system/bootstrap.php(169): CodeIgniter\CodeIgniter->initialize()
#1 /var/www/ci4-strict.tk/public_html/index.php(78): require('/var/www/ci4-st...')
#2 {main}
thrown in /var/www/ci4-strict.tk/system/CodeIgniter.php on line 190
download and make your system directory strict
Reply

#7
this bug has been fixed: https://github.com/codeigniter4/CodeIgni...ssues/2688
Reply

#8
Cool, that was quick!  Cool
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.