May 14, 2014

May 14, 2014
In this article, we are going to discuss about How to load the models inside the another models. A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. In some cases an MVC implementation might instead be "passive," so that other components must poll the model for updates rather than being notified.

This is a really quick help that I'm always looking up is how to load CakePHP Models from within another Model, this is currently using version 2.x.

<?php
// the other model to load & use
App::uses('AnotherModel', 'Model');

class MyModel extends AppModel {

public $name = 'MyModel';

public function test() {
// load the Model
$anotherModel = new AnotherModel();
// use the Model
$anotherModel->save($data);
}
}

Instead of declaring uses at the top you can also import the Model class when you want to use it:

App::import('Model','AnotherModel');
$anotherModel = new AnotherModel();
$anotherModel->save($data);

0 comments:

Post a Comment