0

这让我发疯

当我在 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{

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

在预算控制器中:

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}
4

1 回答 1

1

无需手动创建模型

$this->loadModel('Item');
// now you can access it using
$results = $this->Item->find(...); // etc
于 2013-09-15T02:30:33.117 回答