i am a dynamic drop down noob

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (lcc-srs.assessment_detail, CONSTRAINT assessment_detail_ibfk_1 FOREIGN KEY (assessment_header_id) REFERENCES assessment_header (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

that is the error that occurred when i try to submit the form on assessment detail. i dont know what it means. pls help me decipher it.

here are the codes that i added for dynamic dropdown:

assessment detail controller


array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update','dynamicDesc','dynamicCode'),

				'users'=>array('@'),


.....

public function actionDynamicCode()

	 	         {

	 	                 $data=fee_code::model()->findAll('grade_level_id=:grade_level_id',

 	                                           array(':grade_level_id'=>(int) $_POST['assessment_detail']['grade_level_id']));

	 	 

 	                 $data=CHtml::listData($data,'id','fee_code');

	 	                 foreach($data as $value=>$code1)

	 	                 {

	 	                         echo CHtml::tag('option',

	 	                                            array('value'=>$value),CHtml::encode($code1),true);

	 	                 }

	 	         }

	 	         

	public function actionDynamicDesc()

	 	         {

	 	                 $data=fee_description::model()->findAll('fee_code_id=:fee_code_id',

 	                                           array(':fee_code_id'=>(int) $_POST['assessment_detail']['fee_code_id']));

	 	 

 	                 $data=CHtml::listData($data,'id','fee_desc_code');

	 	                 foreach($data as $value=>$desc1)

	 	                 {

	 	                         echo CHtml::tag('option',

	 	                                            array('value'=>$value),CHtml::encode($desc1),true);

	 	                 }

	 	         }


			),

assessment detail _form


<div class="row">

		<?php echo $form->labelEx($model,'grade_level_id'); ?>

		<?php echo $form->dropDownList($model,'grade_level_id', CHtml::listData(

		grade_level::model()->findAll(), 'id', 'grade_code'),

		array(

		'prompt' => 'Select a grade code',

		'ajax' => array(

		'type' =>'POST',

		'url' => CController::createUrl('assessment_detail/dynamicCode'),

		'update'=>'#'.CHtml::activeId($model,'fee_code_id'),

		))); ?>

		<?php echo $form->error($model,'grade_level_id'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($model,'fee_code_id'); ?>

		<?php echo $form->dropDownList($model,'fee_code_id', CHtml::listData(

		fee_code::model()->findAll(), 'id', 'fee_code'),

		array(

		'prompt' => 'Select a fee code',

		'ajax' => array(

		'type' =>'POST',

		'url' => CController::createUrl('assessment_detail/dynamicDesc'),

		'update'=>'#'.CHtml::activeId($model,'fee_description_id'),

		))); ?>

		<?php echo $form->error($model,'fee_code_id'); ?>

	</div>

 

	<div class="row">

		<?php echo $form->labelEx($model,'fee_description_id'); ?>

		<?php echo $form->dropDownList($model,'fee_description_id', CHtml::listData(fee_description::model()->findAll(), 'id', 'fee_desc_code'),array('prompt' => '- - select fee code - -')); ?>

		<?php echo $form-> error($model,'fee_description_id'); ?>

	</div>

this is the ERD

1073

for the forum.png

You cannot list data twice couse you already do that in actionDynamicCode() and actionDynamicDesc() you have to setup empty array




<div class="row">

                <?php echo $form->labelEx($model,'grade_level_id'); ?>

                <?php echo $form->dropDownList($model,'grade_level_id', CHtml::listData(

                grade_level::model()->findAll(), 'id', 'grade_code'),

                array(

                'prompt' => 'Select a grade code',

                'ajax' => array(

                'type' =>'POST',

                'url' => CController::createUrl('assessment_detail/dynamicCode'),

                'update'=>'#'.CHtml::activeId($model,'fee_code_id'),

                ))); ?>

                <?php echo $form->error($model,'grade_level_id'); ?>

        </div>

        

        <div class="row">

                <?php echo $form->labelEx($model,'fee_code_id'); ?>

                <?php echo $form->dropDownList($model,'fee_code_id',array(),

                array(

                'prompt' => 'Select a fee code',

                'ajax' => array(

                'type' =>'POST',

                'url' => CController::createUrl('assessment_detail/dynamicDesc'),

                'update'=>'#'.CHtml::activeId($model,'fee_description_id'),

                ))

); ?>

                <?php echo $form->error($model,'fee_code_id'); ?>

        </div>

 

        <div class="row">

                <?php echo $form->labelEx($model,'fee_description_id'); ?>

                <?php echo $form->dropDownList($model,'fee_description_id', array()); ?>

                <?php echo $form-> error($model,'fee_description_id'); ?>

        </div>




it does made it look better but the error is still the same :blink:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (lcc-srs.assessment_detail, CONSTRAINT assessment_detail_ibfk_2 FOREIGN KEY (grade_level_id) REFERENCES grade_level (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

lol i didnt read the error :) sec

try this




.....

public function actionDynamicCode()

                         {




$data=fee_code::model()->findAll(array(

'criteria'=>array(

'condition'=>'grade_level_id='.$_POST['assessment_detail']['grade_level_id'],

)

));

                 

                         $data=CHtml::listData($data,'id','fee_code');

                                 foreach($data as $value=>$code1)

                                 {

                                         echo CHtml::tag('option',

                                                            array('value'=>$value),CHtml::encode($code1),true);

                                 }

                         }

                         

        public function actionDynamicDesc()

                         {

                                 $data=fee_description::model()->findAll(array(

'criteria'=>array(

'condition'=>'fee_code_id='.$_POST['assessment_detail']['fee_code_id'],

)

));

                 

                         $data=CHtml::listData($data,'id','fee_desc_code');

                                 foreach($data as $value=>$desc1)

                                 {

                                         echo CHtml::tag('option',

                                                            array('value'=>$value),CHtml::encode($desc1),true);

                                 }

                         }


                        ),




the drop down is not working anymore :o

try echo out the ajax remove the ‘update’=>’#’.CHtml::activeId($model,‘fee_code_id’),

to see if ajax works

did you try putting actionDynamicCode and actionDynamicDesc in the Access rules.




array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update', 'dynamiccode', 'dynamicdesc',),

				'users'=>array('@'),

			),



how do you do that?

yes its there

instead of this




'update'=>'#'.CHtml::activeId($model,'fee_code_id'),



do this




'update'=>'#testid',



or add a jquery success function in some test id , or make on succsess alert(data), and in your action comment the section (below is eg).

and print_r($data);





public function actionDynamicCode()

                         {




$data=fee_code::model()->findAll(array(

'criteria'=>array(

'condition'=>'grade_level_id='.$_POST['assessment_detail']['grade_level_id'],

)

));

                 

                         $data=CHtml::listData($data,'id','fee_code');

print_r( $data);

                         /*     

   foreach($data as $value=>$code1)

                                 {

                                         echo CHtml::tag('option',

                                                            array('value'=>$value),CHtml::encode($code1),true);

                                 }*/

    }