CakePHP call a member function on a non-object

I have the following Model and Controller files, and when I find this url http://....../pois/index, I get this error:

Notice (8): Undefined property: PoisController::$Poi [APP/controllers/pois_controller.php, line 5]

Fatal error: Call to a member function find() on a non-object in /home/joecoyle/public_html/app/controllers/pois_controller.php on line 5

This model is called poi.php:

<?php
class Poi extends AppModel {

}
?>

And the controller is called pois_controller.php

<?php
class PoisController extends AppController {

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

Since I'm new to CakePHP, I'm not sure what causes this error, since everything seems to be called right, and I follow the guide on the CakePHP site ...

thank

+3
source share
4 answers

SpawnCxy ( name , , ), . "Poi" "" , , CakePHP 1.2.6 , :

echo '<p>' . Inflector::singularize( 'Pois' ) . '</p>'; # prints "Pois"
echo '<p>' . Inflector::pluralize( 'Poi' ) . '</p>';    # prints "Pois"

, , Cake PoisController ( ) Poi (), .

+3

var $name = "Poi";, .

PHP5. , .

: : pois_controller.php, :

 <?php
 class PoisController extends AppController
 {
       var $name = "Poi";
       function index()
       {
           debug($this->Poi);
           exit;
       }
 }
 ?>

: pois.Structure: id, name

/pois/ :

Poi Object
(
[name] => Poi
[useDbConfig] => default
[useTable] => pois
[displayField] => name
[id] => 
[data] => Array
    (
    )

[table] => pois
[primaryKey] => id
[_schema] => Array
    (
        [id] => Array
            (
                [type] => integer
                [null] => 
                [default] => 
                [length] => 11
                [key] => primary
            )

        [name] => Array
            (
                [type] => integer
                [null] => 
                [default] => 
                [length] => 11
            )
   ...etc
+4

/ : Cake :

app/config/inflections.php $irregularPlural :

$irregularPlural = array('poi'=>'pois');

"Pois" "Poi".

, / . , , Cake "" "". $this->News->find, $this->New->find, .

0

This is a problem because it was $this->Poinot initialized as an object. I am not familiar with CakePHP, but in your init function in PoisController or in contructor you have to call $this->Poi = new Poi();, so when you try to call the find()method in the index action, it will be called on an instance of the Poi model.

-1
source

Source: https://habr.com/ru/post/1740755/


All Articles