有许多关于将表单中的字段设置为只读的帖子,但似乎没有涵盖hasOne()字段。我希望编辑属于某个班级的学生(注册)列表。该课程是针对特定科目的,例如数学,并存储时间段和学生的最大数量。在编辑注册时,我希望课程信息只能读取,而属于课程的学生可以通过CRUD进行编辑。我可以通过为每个字段调用display()来设置模型的本地字段(例如https://groups.google.com/forum/?fromgroups=#!topic/agile-toolkit-devel/v2xVYsRqFpY),但我找不到设置hasOne的方法。设置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)));
});
另外,如果我设置$这个 - > hasOne( '主题') - >只读(真);在Model_Class中,表单将主题标识显示为文本而不是主题的“名称”。
感谢您的帮助。
您正在使用哪个版本的ATK4?为什么你使用MVCForm,我猜是控制器而不是添加(Form)? 您应该更好地参与关于此Google群组主题中的只读(和可编辑,禁用,隐藏,系统等)字段的讨论。现在在模型中显示(form =>只读)和readonly(true)是两种不同的情况。我只对模型做了一些更改(true),但我们需要讨论如何正确执行。我看到罗马人最近也提出了一些事情。对不起,今晚没时间检查一下。 – DarkSide
我刚刚意识到display(array('form'=>'readonly'))有一个问题,当保存表单时,那些用display(array('form'=>'readonly'))设置的字段被替换为NULL桌子。这在https://groups.google.com/forum/?fromgroups=#!topic/agile-toolkit-devel/v2xVYsRqFpY讨论中有所说明。 – LostInTheWeb
对不起DarkSide,只是看你的评论。我有最新的github版本。谢谢。 – LostInTheWeb