1

有许多关于将表单中的字段设置为只读的帖子,但似乎都没有涵盖 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 显示为文本而不是主题的“名称”。

谢谢你的帮助。

4

1 回答 1

0

在模型内部试试这个:

$this->hasOne('Subject'); // will create 'subject' and 'subject_id' fields
$this->getElement('subject')
    ->editable(true)
    ->display(array('form'=>'readonly'));

hasOne 创建两个字段。取消引用的字段(不带 _id)是默认从表单中排除的计算字段。通过设置“可编辑”,您可以将其返回到表单中(通过它计算的事件)并设置显示属性。或者,您可以这样做:

$forms->getElement('subject_id')->setAttr('disabled',true);

这仍将使用下拉菜单,但将被禁用。我不是 100% 肯定的,但任何 jQueriUI 增强功能(例如自动完成)都必须尊重 disabled 属性。

于 2012-10-26T10:11:00.097 回答