我搜索了所有文档Yii
但没有得到答案。所以我终于来到了这里。我有以下架构
Table Schools
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_name | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
Table Students
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_id | int(10) | NO | FK | | |
| student_name | varchar(100) | NO | | | |
| roll_no | varchar(80) | NO | | | |
| class | varchar(20) | NO | | | | |
| subjects | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
我models and CRUD
为这两个模型做了。在模型中我的关系是这样的
在Students.php
关系就像
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'School' => array(self::BELONGS_TO,'Schools','school_id'),
);
}
在Schools.php
关系就像
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'student' => array(self::HAS_MANY, 'Students', 'school_id'),
);
}
现在我做了,two models rendered in a single page
以便我可以在一个表单中输入所有相应的字段。
In the _form.php file of Students I have made some change in student_name like this
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php echo $form->dropdownList($model,'student_name', CHtml::listData(Students::model()->findAll(), 'id', 'student_name'), array('empty'=>array('Select'=>'--Select One---'))); ?>
<?php echo $form->error($model,'student_name'); ?>
现在对于我得到的这段代码all the student name from the Student model
。所以我的问题是当我从中获取学生姓名dropdown list
并选择一个学生时,它还将获取学生的所有相应值以在_form.php
不点击保存按钮的情况下呈现。这样用户就不必输入再次手动。我认为ajax and json encode
会在这里工作,但不知道如何让他们在这里工作。
[更新]
这是StudentsController
代码
public function actionDisCoor() {
$model = School::model()->findByPk($_POST['Students']['student_id']);
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
}
}
这是_form.php
代码Students
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
?>
<?php echo $form->dropdownList($model,'student_name',$List,
array('onChange'=>CHtml::ajax(array(
'url' => CController::createUrl('DisCoor'),
'type' => 'POST',
'update'=>'#school_id',
)),'style'=>'width:180px;'
)
)?>
<?php echo $form->error($model,'student_name'); ?>
</div>
这是代码accessRules()
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','DisCoor'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
毕竟,当我在萤火虫中看到时,我得到了错误。这是屏幕截图