это сводит меня с умаIs «Item» класс ключевое слово, он не найден в контроллере
Я «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
Вот лог свалка из приложения/TMP/журналы
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}
Почему '$ товар = новый товар();'? Никогда не делайте этого так, всегда используя loadModel() или ClassRegistry :: init(). – mark
@mark: Спасибо, я решил это со следующими строками: $ this-> loadModel ('Item'); $ item = new Item(); – Ashutosh