2013-04-29 2 views
0

Yii의 activerecord-relation-behavior 확장 기능에 문제가 있습니다. , 모델의 행동이 yiiext을 사용Yii ActiveRecord-Relation 모델 저장하지 않음

이 두 모델 사이의 관계는 잘 살고있다 UserPerson (프로파일의 종류),하지만, 난 여전히 연결할 수 없습니다 User 및 하위 모델 :

나는 메인 모델이 . 이러한 맥락 $this에서

내가 실행할 수 있어야, 상기 User 모델을 확장 모델,이 방법으로 RegistrationModel

$person = new \UserPerson(); 
$person->full_name = $this->name; 
$person->birthday = $this->birthday; 
$person->gender = $this->gender; 

$this->person = $person; 
$this->person->save(); 

호출됩니다 $this->save() 을하지만이 오류 얻을 :

You can not save a record that has new related records!

많은 변형을 시도했지만 추한 버전 만 사용했습니다. :(

$person->user_id = $this->id; 
//.. 
$person->save(); 

사람이 문제에 대한 제안이 있습니까

+1

정상 작동하는 방식입니다 .Yii는 자동 저장을 지원하지 않습니다. lated 레코드. 수동으로 저장해야합니다. –

+0

그렇기 때문에 나는 [activerecord-relation-behavior] (https://github.com/yiiext/activerecord-relation-behavior)를 사용하여 이렇게 작업하고 모델 간의 실제 관계를 갖게되었습니다. – seniorpreacher

+0

오, 죄송합니다, 당신이 그 연장을 언급 한 것을 놓쳤습니다. –

답변

1

을 대답은에있는 repository of the extension :

"You can not save a record that has new related records!"

You have assigned a record to a relation which has not been saved (it is not in the database yet). Since ActiveRecord Relation Behavior needs its primary key to save it to a relation table, this will not work. You have to call ->save() on all new records before saving the related record.

그래서 당신은 관련 요소를 추가, 관련 모델을 저장해야하고 그런 다음 모델을 저장하십시오.

$person = new \UserPerson(); 
$person->full_name = $this->name; 
$person->birthday = $this->birthday; 
$person->gender = $this->gender; 
$person->save(); 
//now $person has a primary key 

$this->person = $person; 
$this->person->save(); 
+0

이 경우'$ person'이 유효하고 저장 가능해야합니다. 사용자 ID없이'$ person'을 저장하려고하면'foreign key constraint fail '이납니다. ('$ this') 어쨌든, 나는'$ person'에게 사용자 아이디를 주어야합니까? 이것이 ** activerecord-relation-behavior **가 신경 써야 할 사항이 아닙니까? – seniorpreacher

+0

모델을 저장할 때 sql 데이터베이스에서 id를 제공하지 않지만 id 필드는 자동 증가 특성이 있어야합니다. – darkheir

+0

죄송합니다. 어쩌면 나 자신을 설명 할 수 없을 것입니다. 나는'User'와'UserPerson' 클래스를 가지고 있고'UserPerson'은 외래 키가있는'User'에 속해 있습니다. ('UserPerson.user_id') Btw : PHP는 자동 증가 값이 아닌 모든'id' 필드에 대해 GUID를 생성했습니다. – seniorpreacher