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


Messages In This Thread
undefined model method does not rise error - by carlorfeo - 03-09-2020, 11:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB