Problem with function call in Cakephp

Asked

Viewed 731 times

4

I have this in the Passage.php model:

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

And this in Passagenscontroller.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'));
    }
}?>

The moment I access the Tickets page, an error occurs with that description:

"Error: Call to a Member Function find() on a non-object File: C: wamp www Salepassages app Controller Passagenscontroller.php Line: 8"

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

What can it be?

3 answers

3

Try the following:

In your Passagenscontroller 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'));
   }

}?>
  • This should really solve. I think the problem is that cake does not know that the singular of passages is passage.

  • Exactly, if it was Passage I believe that it would work automatically, but as you used Pt-BR it can not identify.

  • I did this... Look what happens: Error: Table passgems for model Passage was not found in datasource default. Very strange that "passes" !!!

  • @Third The reason is the same. Or you add one public $useTable = 'passagem'; model, or create a new inflection to teach cake to convert singular/plural right.

  • I agree with @bfavaretto. It’s all about the singular/plural that the cake inflection rule doesn’t interpret in English.

  • Fixed!!! Thank you all!!! I put the inflections.php and it worked all right!

Show 1 more comment

1

I don’t know much about cakephp, but from what I read on the site, it says you should use the following:

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

Before the class definition, this would be your Pass.php model.:

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

Modify your Passagenscontroller.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 unladen module. Source: Here

  • The problem is that the error only happens in this Controller. In others this error does not occur.

  • 1

    This is not because it should recognize directly since this controller belongs to the module.

  • This is following the normal cakephp standards. Since the class nomenclature is correct (if using the 'Cakeptbr' plug) then the model would be loaded automatically.

0

Solution

Failed to add

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

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

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

Motive

App::uses('Appmodel', 'Model') ensures that the Model will be loaded (Lazy load) at each instance it is called.

Error of the Description

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

  • The problem is that the error only happens in this Controller. In others this error does not occur. I tried to use this method you mentioned. I was unsuccessful.

  • may only be that, not loaded as quoted in the reply, try using another name in the class, to see if the error persists.

  • I put another name in the class. Unsuccessful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.