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);
}
}

2 comments:

Rachel said...

Hey...just dropping by to say hi!

HAppy New Year to ya! Hope you have a fruitful year ahead!

Cheers!

Jason Liu said...

Hi!, happy new year :)