CakePHP 4 - Loading a Model Within a Component

CakePHP 4 - Loading a Model Within a Component

This example shows you how to use a model (e.g. $this->Load('Model') ) from within a component, so you can use models like you would in a native controller.

First, create your component with use:

use Cake\Datasource\ModelAwareTrait;

Next, you're ready to load any models you have from within any function inside your component class:

class myComponent extends Component {

    use ModelAwareTrait;

     function myFunction() {
          ...

          $this->loadModel('CartItems');

          ...

     }
}

Share this Post