Google

Wednesday, December 23, 2009

CakePHP - Load Model within Model

There are times that we want to link-up the model, but not the "hard way" example: hasOne, hasMany, belongsTo etc.
When you are in such situation the code below might be able to help you.

We can actually use the model function by using ClassRegistry class.
Below are the example:
class ThisModel extends AppModel {
var $name = "ThisModel";

function somename($id){
//note that "ThatModel" does not belongs to "ThisModel"
$rows = ClassRegistry::init("ThatModel")->findById($id);
}
}