有许多关于将表单中的字段设置为只读的帖子,但似乎都没有涵盖 hasOne() 字段。我希望编辑属于给定班级的学生(注册)列表。该课程针对特定科目,例如数学,并存储时间段和最大学生人数。在编辑注册时,我希望班级详细信息为只读,而属于班级的学生可通过 CRUD 进行编辑。我可以通过为每个字段调用 display() 来设置模型的本机字段(例如https://groups.google.com/forum/?fromgroups=#!topic/agile-toolkit-devel/v2xVYsRqFpY),但我不能找到一种设置 hasOne 的方法。
以下是我到目前为止的代码。
学科模型
class Model_Subject extends Model_Table {
public $table='subject';
function init(){
parent::init();
$this->addField('name');
$this->addField('subject_code');
$this->addField('semester');
$this->addField('description');
}
}
类模型
class Model_Class extends Model_Table {
public $table='class';
function init(){
parent::init();
$this->hasOne('Subject');
$this->addField('date_start')->type('date')->caption('Start');
$this->addField('date_end')->type('date')->caption('End');
$this->addField('max_students')->type('int');
$this->hasMany('ClassHasStudent','class_idclass', 'idclass');
}
}
学生模型
class Model_Student extends Model_Table {
public $table='student';
function init(){
parent::init();
$this->hasMany('ClassJoinClassHasStudent');
$this->addField('student_ID')->caption('Student ID');
$this->addField('name')->caption('Name');
$this->addExpression('number_classes')->set(
$this->add('Model_ClassHasStudent')->addCondition('student_id',$this->_dsql()->getField('id'))->count()
)->caption('Number of classes');
}
}
链接表。
class Model_ClassHasStudent extends Model_Table {
public $table='class_has_student';
function init(){
parent::init();
$this->hasOne('Class', 'class_id', 'id');
$this->hasOne('Student');
$this->addField('date_enrolled')->type('date');
$this->addField('grade');
}
}
形式是:
$form=$this->add('MVCForm');
$classes=$this->add('Model_Class');
// $classes-> set hasOne Subject name field to read only.
$classes->getField('date_start')->display(array('form'=>'readonly'));
$classes->getField('date_end')->display(array('form'=>'readonly'));
$classes->getField('max_students')->display(array('form'=>'readonly'));
// Method from Romans.
$form->model->load($id);
$this->add('CRUD')->setModel($form->model->ref('ClassHasStudent'));
$form->addSubmit('Save');
$form->onSubmit( function($form){
$form->update();
return $form->js()->univ()->location($form->api->getDestinationURL(
'managestudents',
array('id'=>false)));
});
另外,如果我设置 $this->hasOne('Subject')->readonly(true); 在 Model_Class 中,表单将主题 ID 显示为文本而不是主题的“名称”。
谢谢你的帮助。