I'm very new to Yii framework
and so far I enjoy a lot working with it. I'm at my first project on my own and I'm kinda stuck on the following issue.
I have two models Student
and Teacher
. They are not related with a foreign key, but student table has a teacher_id
field that belongs to the Teacher
table. (not very clear yet how to relate them, but that's another story I will have to worry about later).
When updating the Student in the view using the form, I incorporated the form for the teacher as well (full name, age, etc), but being part of the Student form, it does not benefit from the validation rules set in Student model. When I save the Teacher info (during the same POST request, before saving the Student fields), the validation seems to work, because it won't save it if a required field is missing, but it doesn't return to the Student form, the validation errors like it does for it's own required fields. I hope this is not too confusing, but I don't know how better to explain it.
Any ideas are more than welcome!
You can declare relationships with this format in the model file, there need not be a foreign key to declare relationships, but your queries will perform faster if there is one ...
'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
an example for your case could be something like ..
public function relations() {
return array(
'student' => array(self::BELONGS_TO, 'teacher', 'teacher_id'),
);
}
With respect to your query on using multiple models in the same form, it is not very clear from your description what exactly is happening, from what I understood you are using multiple table_models in the same form, while this is possible it is quite complex.
I suggest you create a separate form model for handling multiple table entries using single form
Not sure I understand your question correctly, but if you want to validate and save two models in the same form you can do it like this.
Controller:
$teacherModel = new Teacher;
$studentModel = new Student;
if(isset($_POST['Teacher']) && isset($_POST['Student']))
{
$teacherModel->attributes = $_POST['Teacher'];
$studentModel->attributes = $_POST['Student'];
// check both models for validation errors before saving and redirecting.
$teacherValid = $teacherModel->validate();
$studentValid = $studentModel->validate();
if($teacherValid && $studentValid)
{
$teacherModel->save(false); // false = don't perform validation before saving
$studentModel->save(false);
$this->redirect(array('view','id'=>$studentModel->id));
}
}
$this->render('create',array(
'teacherModel'=>$teacherModel,
'studentModel'=>$studentModel,
));
This will make sure both models are valid before redirecting, or else it will return to the form.
You can then display validation errors from both models in your view by passing them in an array to the method $form->errorSummary:
<?php echo $form->errorSummary(array($teacherModel, $studentModel)); ?>
So I tried this (what Christian suggested) and while the validation seems to work, the errorSummary method doesn't do it's job. If I miss a required field on the Teacher form, it just doesn't save it, but it doesn't return the error summary. What I noticed interesting, is if in I miss a required field in the Student form, it gives me the nice red error fields, but after I enter something in those fields and I omit one rquired field in the Teacher form, this time the error Summary for the Teacher, does show. It was a bit confusing. In the end I took another route. I am checking first the Teacher form and if it doesn't validate, I loop through the Teacher->model()->errors , create a unordered list, and send it as a flash message to the viewer. So far it works OK that way. Maybe in the future, as I become more knowledgeable, I can return and improve this code.