Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kinshara's avatar

model orderBy method not exist

i have my parent controller

class Parent {
    public $model;

    public function test() {
        return $this->model->orderBy();
    }
}
//and then my controller
class foo extends Parent {
    public function index() {
        $this->model = new homeModel;
    }
}

i got that orderBy method not found

0 likes
4 replies
phildawson's avatar

Well the issue would be if you call test then $this->model would be null.

Here's the same pseudo code replacing Parent which is a reserved keyword

class Bar {
    public $model;

    public function test() {
        return $this->model->orderBy();
    }
}
//and then my controller
class foo extends Bar {
    public function index() {
        $this->model = new homeModel;
    }
}

Mocking the class and orderBy method

class homeModel {
    function orderBy(){return 'order!';}
}

index() would need to be called before test() to set the model attribute to the new homeModel

$foo = new foo;
$foo->index();
echo $foo->test(); // order!

see @bobbybouwmann below for a solution to your issue.

bobbybouwmann's avatar
Level 88

You have to set the model in constructor!

class Parent {

    public $model;

    public function __construct()
    {
        $this->model = new MyModel;
    }

    public function test()
    {
        return $this->model->orderBy();
    }

}

class Foo extends Parent {

    // You can override the model if you wish here
    public function __construct()
    {
        parent::__construct();

        $this->model = new OrderModel;
    }   

}
1 like
kinshara's avatar

@bobbybouwmann lol! it works thanks bro.. i realized it actually, but i use wrong Model thats why it wont found any method.. lol..

@phildawson well, the methods are inherited from Models controller..

phildawson's avatar

@kinshara I had assumed that. Sorry if I made that unclear with "Mocking the class and orderBy method". Regardless of framework if you had an empty php file and ran it the following it would illustrate the issue of the object needing to be init is what I was getting at.

<?php

class Bar {
    public $model;

    public function test() {
        return $this->model->orderBy();
    }
}
//and then my controller
class foo extends Bar {
    public function index() {
        $this->model = new homeModel;
    }
}

class homeModel {
    function orderBy(){return 'order!';}
}

$foo = new foo;
$foo->index();
echo $foo->test();

Please or to participate in this conversation.