I'm using CakePHP, and trying to pull data from a table outside my current controller. The only thing I can find online is about using associations and belongsTo and such, however these two items shouldn't be related. I simply want to pull data from another table at will.
4 Answers
You could also use the loadModel inside your functions.
$this->loadModel('User', 2);
$user = $this->User->read();
-
Thanks to both of you for the answers. I tried both, but am getting an error in which the function (from the other model) is being sent as an SQL query instead of being executed as a function. It loads fine either way... it just keeps sending the function as a query. I've tried looking this up online and someone said it could be caused by CakePHP not finding the model, but creating one still has no effect.– JasonApr 27, 2011 at 3:48
-
-
Well if you want to access different model (not the related to the controller) you can use
class SomeController extends AppController {
var $uses = array('Model1', 'Model2');
// and then later in the code you can use them like this
function index() {
$this->Model1->doStuff;
$this->Model2->doStuff;
}
}
-
-
I agree that this will get the things solved but the better method is
$this->loadModel()
– RSKApr 27, 2011 at 4:36
either
//only controller
$this->loadModel('MyModel');
$res = $this->MyModel->find(...)
or
//everywhere
$this->MyModel = ClassRegistry::init('MyModel');
$res = $this->MyModel->find(...)