Error: calling function member function () for non-object in cakephp controller

I am new to cakephp and tried to generate some CRUD operations using a console tool. It works fine except for one table (the largest).

When trying to add a new element, it returns:

Error: calling function member function () on a non-object File: C: \ wamp \ www \ cakephp \ app \ Controller \ ChantiersController.php
Line: 50

Here is line 50 onwards:

$programs = $this->Chantier->Program->find('list'); $etats = $this->Chantier->Etat->find('list'); $types = $this->Chantier->Type->find('list'); $champsLibres = $this->Chantier->ChampsLibre->find('list'); $feuillesDeRoutes = $this->Chantier->FeuillesDeRoute->find('list'); $directionsPilotes = $this->Chantier->DirectionsPilote->find('list'); $this->set(compact('programs', 'etats', 'types', 'champsLibres', 'feuillesDeRoutes', 'directionsPilotes')); 
+4
source share
2 answers

TL; DR / Answer:

You can either fix the association or load the model and delete the end-to-end call (part ->Chantier :

 $this->loadModel('Program'); $programs = $this->Program->find('list'); 

Details / Explanation:

This error basically means that you are trying to call find() on a model that is not loaded into the controller.

By default, the controller model is loaded. And, as you do this, you can use the THROUGH model for a loaded model. (if associations are configured correctly).

For instance:

 //ChantiersController $this->Pizza->find('all'); //ERROR - Pizza model isn't loaded 

To solve this problem, just load the model before trying it:

 $this->loadModel("Pizza"); $this->Pizza->find('all'); //All is good - loaded on the line above 

In your case, since it looks like you are using associations with the Chantier model to search through other models, the relationship between the two models is probably incorrect.

+14
source

If you do not want to use the loadModel () function, you can define

 var $uses = array('Program'); 

You can also specify other model names also in the array

0
source

All Articles