Problem with function call in CakePHP

4

I have this in the Pass.php template:

<?php
class Passagem extends AppModel {
    public $name = 'Passagem';
}?>

And this in PasswordsController.php:

<?php
class PassagensController extends AppController {
    public $helpers = array('Html','Form');
    public $name = 'Passagens';
    public $components = array('Session');

    function index(){
        $this->set('passagens', $this->Passagem->find('all'));
    }
}?>

When I access the Passages page, an error occurs with this description:

  

"Error: Call a member function find () on a non-object   File: C: \ wamp \ www \ SalesPassages \ app \ Controller \ PasswordsController.php   Line: 8 "

Note: In the project there are other Models and Controllers. But only in "Passes" this error occurs.

What can it be?

    
asked by anonymous 29.01.2014 / 03:59

3 answers

3

Try the following:

In your PassportController add the $ uses as below:

<?php
class PassagensController extends AppController {
   public $helpers = array('Html','Form');
   public $name = 'Passagens';
   public $components = array('Session');
   public $uses = array('Passagem');

   function index(){
      $this->set('passagens', $this->Passagem->find('all'));
   }

}?>
    
29.01.2014 / 13:16
1

I do not understand much of cakephp, but from what I read on the site, it says that one should use the following:

App::uses('AppModel', 'Model');

Before defining the class, it would look like your model Pass.php:

<?php
App::uses('AppModel', 'Model');
class Passagem extends AppModel {
    public $name = 'Passagem';
}?>

Modify your PasscodeController.php code to:

<?php
class PassagensController extends AppController {
    public $helpers = array('Html','Form');
    public $name = 'Passagens';
    public $components = array('Session');

    function index(){
        $this->loadModel('Passagem');
        $this->set('passagens', $this->Passagem->find('all'));
    }
}?>

The problem is that you are trying to call the function of an unloaded module. Source: Here

    
29.01.2014 / 04:09
0

Solution

Missed adding

App::uses('AppModel', 'Model');

At the start of the Model, it would look like this:

<?php
App::uses('AppModel', 'Model');
class Passagem extends AppModel {
    public $name = 'Passagem';
}
?>

Reason

App :: uses ('AppModel', 'Model') ensures that the Model will be loaded (lazy load) on every instance it is called.

Description Error

As the Passing property was not defined (not related to the Model), the use of a method in a property that is not an object in itself causes such error

    
29.01.2014 / 04:15