2014-10-04 79 views
1

我使用CakePHP和我得到了以下错误:调用一个成员函数查找()一个非对象

Error: Call to a member function find() on a non-object 
File: C:\xampp\htdocs\project\app\Controller\TripsController.php  
Line: 12 

Tripscontroller.php代码:

<?php 

class TripsController extends AppController{   

    public $name= 'Trip'; 

    public function index($City=null){ 
     $trips = $this->Trip->find('all'); 
     $this->set('trips',$trips); 

     $cities = $this->Trip->City->find('all');   
     $this->set('cities',$cities); 
    } 
} 

我已经已关 “trips.php” 里面的两个对象:

<?php 
class Trip extends AppModel 
{ 
    public $name= 'Trip'; 
    public $belongsTo= array('City'); 
} 

?> 
+0

您需要添加一个'__construct()'函数和给予'$ this' – iatboy 2014-10-04 02:20:58

+1

http://book.cakephp.org/2.0/en/getting-started /cakephp-conventions.html#file-and-class-name-conventions,http://book.cakephp.org/2.0/en/controllers.html#Controller::$name – ndm 2014-10-04 02:22:27

+1

什么是 “trips.php”?这不符合任何惯例。 – Dave 2014-10-04 20:35:18

回答

-1

尝试: -

public $uses = array('Trip');
in your controller

+0

伙计,行程已按每OP按关系连接。检查属于问题。所以不需要隐式使用'public $ uses' – 2014-10-08 07:41:56

0

HEHA :)内部控制

public $name= 'Trips'; 

是正确的ü有Trip,记住控制器使用复数名称。

而且模型文件名=>Trip.php

相关问题