Yii Framework Forum: Activerecord Attributes And Setting Foreign Keys - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activerecord Attributes And Setting Foreign Keys

#1 User is offline   charlespwd 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-May 13

Posted 19 December 2013 - 04:14 PM

I have a unit test that tries to save a record in a table with three foreign keys. But somehow that test failed unless I explicitly assigned the foreign keys via the accessors.

The test below passes with two assertions

public function testSaveViaAttributesWithForeignKeys(){
	$employee = $this->emp('student1');
	$timeEntry = new TimeEntry();
	$timeEntry->attributes = array(
		'student_id' => $employee->id, 
		'date' => 1387152000, 
		'hours_worked' => 8.00, 
		'hours_type_id' => 1, 
		'status_id' => 1
	);
	try {
		$timeEntry->save();
	} catch (Exception $e) {
		$timeEntry->student_id = $employee->id;
		$timeEntry->hours_type_id = 1;
		$timeEntry->status_id = 1;
		$this->assertTrue($timeEntry->save());
		$this->assertTrue(isset($e));
	}
}


Is this a bug or should I not specify foreign keys via the attributes array?
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 19 December 2013 - 04:29 PM

Assigning the attributes uses the setAttributes setter, which by default, sets only safe attributes. Apparently you have some rules that makes those FKs unsafe.
Try calling setAttributes($attributes, false) or adjust your rules.
Don't be a dick.
0

#3 User is offline   charlespwd 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-May 13

Posted 19 December 2013 - 04:47 PM

View Postnineinchnick, on 19 December 2013 - 04:29 PM, said:

Assigning the attributes uses the setAttributes setter, which by default, sets only safe attributes. Apparently you have some rules that makes those FKs unsafe.
Try calling setAttributes($attributes, false) or adjust your rules.


Thanks a lot that fixed it.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users