親を持つ新しいレコードの挿入に問題があります。
<?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」はその主キーです。
ここでの問題は、B のモデル オブジェクトを保存しようとしたとき ($b->save())、'id' を除くすべての属性を設定した後 (ただし、オブジェクトのプロパティ 'a' は [$b] で設定されます)たとえば、モデル「A」のアクティブ レコードが主キー 10 を持つとします)、「id」が設定されていないため、レコードの挿入時に例外がスローされます。しかし、モデルオブジェクトの「id」を設定した後に同じことを試みたところ、正しく挿入されました。関連するプロパティが設定されている場合でも、子モデルの外部キー属性を設定する必要があるのはなぜですか? これに対する解決策はありますか。外部キー参照 ID が関連モデル obj から自動的に取得されるようにするには?
前もって感謝します。