Welcome Guest, Not a member yet? Register   Sign In
undefined model method does not rise error
#1

(This post was last modified: 03-11-2020, 03:12 AM by carlorfeo.)

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...
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
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

(This post was last modified: 03-11-2020, 04:09 AM by InsiteFX.)

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

(This post was last modified: 03-10-2020, 08:55 PM by John_Betong.)

@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
Reply
#7

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

Cool, that was quick!  Cool
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB