My code looks like this:
$newWorkingPeriod = new EmployeeWorkingPeriods();
$form['AddTime'] = &$newWorkingPeriod;
if (isset($_POST['AddTime'])) {
$newWorkingPeriod -> setAttributes($_POST['AddTime']);
$newWorkingPeriod -> validate();
if (!$newWorkingPeriod -> hasErrors()) {
$newWorkingPeriod -> save();
// Flash user.
// Redirect.
}
}
// ...
EmployeeWorkingPeriods is a automaticly generated model from a DB and all of the other attributes but the foreign key (employeeId) inside EmployeeWorkingPeriods is set to the given input from $_POST[‘AddTime’]. But if I go and change the rules for the model from:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('startTime, endTime', 'numerical', 'integerOnly'=>true),
);
}
To this:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('employeeId, startTime, endTime', 'numerical', 'integerOnly'=>true),
);
}
Then the employeeId is set as I think it should be. But why do I need to change this, that would be very irritating to do that for all the models that have a foreign key that I’d like to set with the setAttributes() method.