"관련 모델 데이터 저장 (hasOne, hasMany, belongsTo)"으로 시작하는 섹션의 http://book.cakephp.org/2.0/en/models/saving-your-data.html에 예제 코드를 작성하려고합니다. 그러나, 나는 내가 해제 호출 다음과 같은 오류 메시지가 받고 있어요 :관련 모델을 CakePHP로 저장
과부하 속성 AppModel이의간접 수정 :: $ MealContent 효과가 없습니다 [APP \ 컨트롤러 \ MealsController.php, 라인 (15)]의 속성을 수정하는 시도 비 개체 [APP \ Controller \ MealsController.php, line 15]
당연히 데이터도 저장되지 않습니다.
본인의 애플리케이션에는 회사 또는 계정이 없습니다. 대신, 나는 식사와 식사 콘텐츠를 가지고 있지만 관계는 똑같이 설정되어있는 것 같습니다. 분명히, 어딘가에이라는 문제가 있습니다.하지만 여기에 몇 가지 코드가 있습니다.
Meals.php :
class Meals extends Entry {
public $hasMany = 'MealContents';
public $validate = array(
'Timestamp' => array(
'validDate' => array(
'rule' => array('garbageDateChecker'),
'required' => true,
'message' => 'The date could not be understood.'),
'noTimeTravel' => array(
'rule' => array('noTimeTravel'),
'required' => true,
'message' => 'You cannot enter times in the future.')
)
);
}
MealContents.php :
class MealContents extends Entry {
public $belongsTo = 'Meals';
public $validate = array(
'Meals_ID' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'This field cannot be blank')
),
'Item_ID' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'This field cannot be blank')
)
);
}
그리고 마지막으로, 상기 제어부의 인덱스() 함수 :
public function index() {
$this->set('title_for_layout', "Food Log");
if ($this->request->is('post')) {
$this->Meal->create();
unset($this->Meal->MealContent->validate['Meals_ID']);
if ($this->Meal->saveAssociated($this->request->data)) {
$this->Session->setFlash('The meal was logged.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash("Couldn't save the meal.");
}
}
}
Entry.php
abstract class Entry extends AppModel {
public function garbageDateChecker($dateToCheck) {
date_default_timezone_set("America/Tijuana");
$result = TRUE;
try {
new DateTime($dateToCheck['Timestamp']);
} catch (Exception $e) {
$result = FALSE;
}
return $result;
}
public function noTimeTravel($dateToCheck) {
date_default_timezone_set("America/Tijuana");
$result = TRUE;
$objectifiedDateToCheck = new DateTime($dateToCheck['Timestamp']);
$currentTime = new DateTime("now");
if ($objectifiedDateToCheck > $currentTime) {
$result = FALSE;
}
return $result;
}
}
을
유효성 검사 규칙 및 unset() 행을 주석으로 처리하더라도 데이터가 저장되지 않기 때문에 save() 오류가 유효성 검사로 인해 발생하지 않았 음을 확신합니다.
불량 데이터 또는 엉망진창으로 쉽게 비난 할 수 있습니다. 그러나 데이터는 정상적으로 보입니다.
$this->request->data
--MealContents
---[0]
-----[Food_Item_ID] = "0"
--Meals
---[Comments] = ""
---[Timestamp]
-----[day] = "12"
-----[hour] = ...
CakePHP 서적을 읽었을 때 내가 놓친 부분은 무엇입니까?
실제로 저는 AppModel을 확장 중입니다. 설명하기 위해 게시물에 Entry 클래스를 포함 시켰습니다. 다른 제안에 대해 추가 의견을 남깁니다. –
모든 유효성 검사를 해제하지 않는 것이 좋습니다. –
식사 테이블에 행이 올바르게 표시되지만 Meal_Contents 테이블은 비어 있습니다. –