'Item'类是关键字,它在控制器中找不到

时间:2013-09-14 12:14:17

标签: cakephp model controller

这让我发疯了

我有两个型号'Item'& “预算”,因为我在cakephp中创建了预算管理项目:

<?php
class Item extends AppModel{ var $name = "Item"; }
?>

<?php
class Budget extends AppModel {  var $name = "Budget";  }
?>

我有以下控制器:

<?php
class BudgetsController extends AppController{

&安培;

<?php
class ItemsController extends AppController{

现在,我正在努力节省预算和物品。以下是代码:

在BudgetsController中:

function savebudget(){

        if($this->request->isAjax()){       

            $budgets = $this->Budget->find('all', array(
                                        'conditions'=>array(
                                                'Budget.username'=>$this->Session->read('loggeduser'),
                                                'Budget.budgetmonth'=>$this->request->data['budgetmonth'],
                                                'Budget.budgetyear'=>$this->request->data['budgetyear']
                                            )
                                    ));

            $found = false;

            foreach($budgets as $ob){
                $found = true;
                break;
            }

            if ($found) {
                $this->layout = false;
                $this->render(false);
                echo "Duplicate budget...";
            }
            else{
                $budget = new Budget();

                $budget->username = $this->Session->read("loggeduser");
                $budget->budgetyear = $this->request->data['budgetyear'];
                $budget->budgetmonth = $this->request->data['budgetmonth'];
                $budget->amount = $this->request->data['amount'];

                if ($this->Budget->save ( $budget )) {
                    $this->layout = false;
                    $this->render(false);
                    echo "Budget set for this month!";
                } else {
                    $this->layout = false;
                    $this->render(false);
                    echo "Error setting budget!";
                }
            }
        }

=============================================== ======
在ItemsController中:

function saveitem(){

        if ($this->request->isAjax()) {

            $item = new Item();             

            $item->username = $this->Session->read('loggeduser');
            $item->itemname = $this->request->data ['itemname'];
            $item->cost = $this->request->data ['cost'];
            $item->entrydate = date('Y-m-d');

            if ($this->Item->save ( $item )) 
                echo "Item added...";
            else
                echo "Error in adding item...";

            $this->layout = false;
            $this->render(false);
            echo "";
        }
        else{
            $this->layout = false;
            $this->render(false);
            echo "";
        }
    }   

问题出在ItemsController中,它给出了找不到错误“Item”的类 而它在BudgetController中的工作正常


这是来自app / tmp / logs

的日志转储
2013-09-14 14:10:51 Error: Fatal Error (1): Class 'Item' not found in     
[C:\xampp\htdocs\budget\app\Controller\ItemsController.php, line 8]
2013-09-14 14:10:51 Error: [FatalErrorException] Class 'Item' not found
Request URL: /budget/saveitem
Stack Trace:
#0 C:\xampp\htdocs\budget\lib\Cake\Error\ErrorHandler.php(184): ErrorHandler::handleFatalError(1,    
'Class 'Item' no...', 'C:\xampp\htdocs...', 8)
#1 [internal function]: ErrorHandler::handleError(1, 'Class 'Item' no...', 'C:\xampp\htdocs...', 8, Array)
#2 C:\xampp\htdocs\budget\lib\Cake\Core\App.php(931): call_user_func('ErrorHandler::h...', 1, 'Class 'Item' no...', 'C:\xampp\htdocs...', 8, Array)
#3 C:\xampp\htdocs\budget\lib\Cake\Core\App.php(904): App::_checkFatalError()
#4 [internal function]: App::shutdown()
#5 {main}

1 个答案:

答案 0 :(得分:1)

无需手动创建模型

$this->loadModel('Item');
// now you can access it using
$results = $this->Item->find(...); // etc