부모가있는 새 레코드를 삽입하는 데 문제가 있습니다.관련 항목 저장 (일대일) 부모의 참조가있는 YII 모델
<?php
/**
* This is the model class for table "tbl_a".
* @property integer $id
....
*/
class A extends CACtiveRecord{
}
?>
<?php
/**
* This is the model class for table "tbl_b".
* @property integer $id
....
* The followings are the available model relations:
* @property A $a
*/
class B extends CACtiveRecord{
//codes...
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'a' => array(self::BELONGS_TO , 'A', 'id'),
);
}
}
?>
이것은 내 모델 클래스의 구조입니다. 여기서 tbl_b의 'id'는 기본 키로 설정되고 tbl_a를 참조하는 외래 키입니다. tbl_a의 'id'는 기본 키입니다.
내 문제는 'id'를 제외한 모든 속성을 설정 한 후에 B ($ b-> save()) 모델 개체를 저장하려고했을 때입니다.하지만 개체 [a ]가 기본 키 10을 갖는 모델 'A'의 활성 레코드로 설정되면 'id'가 설정되지 않아 레코드 삽입시 예외가 발생합니다. 하지만 모델 객체의 'id'를 설정 한 후에 똑같이 시도해 보았을 때 올바르게 삽입되었습니다. 관련 속성이 설정되어 있어도 하위 모델의 외래 키 특성을 설정해야하는 이유는 무엇입니까? 이 문제에 대한 해결책이 있습니까? 그래서 외래 키 참조 ID는 관련 모델 obj에서 자동으로 취해진 다.
미리 감사드립니다.