如何保存多个复选框值,这些值在cakephp中检查hasMany关系?

时间:2013-02-24 20:26:45

标签: cakephp-2.0

假设我有3个表property_detailsproperty_feature_detailsproperty_feature_relations

PropertyDetail Model与PropertyFeatureRelation具有hasMany关系。

保存我的数据数组时看起来像这样:

$this->data = array('PropertyDetail' => array('name'=>'xyz'),'PropertyFeatureRelation' => array('0' => array('feature_id'=>1),'1' => array('feature_id'=>5),'2' => array('feature_id'=>0),'3' => array('feature_id'=>0)));

这些feature_ids值来自复选框。检查它们包含id值和未检查具有0值。但是在子表中保存所有数据零和非零。

实际上我想保存那些仅选中的复选框值。请不要提供任何手动控制器逻辑。帮助我。

1 个答案:

答案 0 :(得分:0)

我正在解释你的问题,答案及其调查的例子

您可以使用多个复选框语法,如下所示:

                                             $answers=array();
                                    $tmp_ans=$question['Answer'];
                                    for($j=0;$j < count($tmp_ans);$j++)
                                    $answers[$tmp_ans[$j]['id']]=$tmp_ans[$j]['answer'];            
                                    echo '<li>';
                                        echo $this->Form->input('Answer', array(
                                        'type' => 'select',
                                        'name'=>'Answer['.$question['Question']['id'].']',
                                        'class'=>'test',
                                        'label'=>false,
                                        'multiple' => 'checkbox',
                                        'options' =>$answers ,
                                        ));

                                    echo '</li>';

之后你将只有那些已经检查过的答案,其他人不会出现在帖子数组中,所以为了保存数据,你可以在句法下面使用

if(!empty($this->data['Answer'])){

                foreach ($this->data['Answer'] as $key=>$val):

                    if(!empty($val)):
                            if(is_array($val)):

                                foreach ($val as $ans):
                                    $post_data['UserAnswer']['id']='';
                                    $post_data['UserAnswer']['survey_id']=$id;
                                    $post_data['UserAnswer']['user_id']=$this->Auth->user('id');
                                    $post_data['UserAnswer']['question_id']=$key;
                                    $post_data['UserAnswer']['answer_id']=$ans;
                                    $this->UserAnswer->save($post_data['UserAnswer']);
                                endforeach;
                            else:
                                    $post_data['UserAnswer']['id']='';
                                    $post_data['UserAnswer']['survey_id']=$id;
                                    $post_data['UserAnswer']['user_id']=$this->Auth->user('id');
                                    $post_data['UserAnswer']['question_id']=$key;
                                    $post_data['UserAnswer']['answer_id']=$val;
                                    $this->UserAnswer->save($post_data['UserAnswer']);

                            endif;

                    endif;
                endforeach;

            }