すべてのドキュメントを検索しまし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
、それぞれのフィールドすべてを 1 つのフォームに入力できるようになりました。
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('*'),
),
);
}
結局、firebugで見たときにエラーが発生しました。スクリーンショットは次のとおりです