保存多个在输入中选择 [英] Saving Multiple Select in input

查看:74
本文介绍了保存多个在输入中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有




  • 用户(id,姓名)

  • name)

  • section_users(id,user_id,section_id)



管理员添加所有用户和部分。一旦添加这些,我想让管理员选择部分,并添加其中的所有用户在section_users






选择多个设置为true的输入。如何以cakephp方式和验证保存此数据。

 <?php echo $ this-> Form-> ; input(section_id); ?> 
<?php echo $ this-> Form-> input(user_id,array('multiple'=>'checkbox')); ?>

这会生成

  Array 

[section_id] => 1
[user_id] => Array

[0] =& b $ b [1] => 4



我知道我可以循环并转换为这个,并使用saveAll或saveMany,但什么是cakephp方式/正确的方式来做。

  Array 

[0] => Array

[section_id] => 1
[user_id] => 3

[1] => Array

[section_id] => 1
[user_id] => 4




解决方案

请阅读文档,请阅读它们,如果你不理解它们(这是可以理解的,因为HABTM部分有点混乱,需要一些试验和错误),告诉我们你有什么问题。



http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm



根据显示的示例,将多个X保存到Y的格式应为

  Array 

[Section] => array

[id] => 1

[User] => array

[User] => Array(3,4)



对应的表单可能如下所示:

 <?php echo $ this - > Form-> create('User'); ?> 
<?php echo $ this-> Form-> input('Section.id'); ?>
<?php echo $ this-> Form-> input('User',array('multiple'=>'checkbox')) ?>
<?php echo $ this-> Form-> end('Add Users'); ?>

并且数据将通过模型,这样它的修改列正在更新。

  public function addUsersToSection($ id){
// ...
if($ this-> request-> is('post')){
if($ this-> ; Section-> save($ this-> request-> data)){
// ...
} else {
// ...
}
} else {
$ options = array(
'conditions'=> array(
'Section。'。$ this-> Section-> primaryKey => $ id

);
$ this-> request-> data = $ this-> Section-> find('first',$ options);
}

$ users = $ this-> Section-> User-> find('list');
$ this-> set(compact('users'));
}

另一种方法是重构数组,如Vinay Aggarwal所示精细,唯一的区别是它需要通过连接模型进行保存,因此不会更新模型修改栏。


I have model for

  • user(id,name)
  • section(id,name)
  • section_users(id,user_id,section_id)

The admin adds all the users and sections separately. Once theses are added I want the admin to selects the section and add all the users in it in section_users


I have a select input with multiple set to true. How do i save this data the cakephp way along with validation.

<?php echo $this->Form->input("section_id"); ?>
<?php echo $this->Form->input("user_id", array('multiple'=>'checkbox')); ?>

This generates

Array
(
    [section_id] => 1
    [user_id] => Array
        (
            [0] => 3
            [1] => 4
        )

)

I know i can loop and convert to this and use saveAll or saveMany but what is the cakephp way/right way to do it.

Array
(
    [0] => Array
        (
            [section_id] => 1
            [user_id] => 3
        )
    [1] => Array
        (
            [section_id] => 1
            [user_id] => 4
        )

)

解决方案

As already mentioned, this is exaplained in the docs, please read them, and in case you don't understand them (which would be understandable as the HABTM section is a little confusing and requires some trial & error), tell us what exactly you are having problems with.

http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

Based on the examples shown, the format for saving multiple X to Y should be

Array
    (
        [Section] => Array
            (
                [id] => 1
            )
        [User] => Array
            (
                [User] => Array(3, 4)
            )
    )

The corresponding form could look like this:

<?php echo $this->Form->create('User'); ?>
    <?php echo $this->Form->input('Section.id'); ?>
    <?php echo $this->Form->input('User', array('multiple' => 'checkbox')); ?>
<?php echo $this->Form->end('Add Users'); ?>

And the data would be saved via the Section model, that way its modified column is being updated properly.

public function addUsersToSection($id) {
    // ...
    if($this->request->is('post')) {
        if($this->Section->save($this->request->data)) {
            // ...
        } else {
            // ...
        }
    } else {
        $options = array(
            'conditions' => array(
                'Section.' . $this->Section->primaryKey => $id
            )
        );
        $this->request->data = $this->Section->find('first', $options);
    }

    $users = $this->Section->User->find('list');
    $this->set(compact('users'));
}

Another way would be to restructure the array as shown by Vinay Aggarwal, that works fine, the only difference is that it requires saving through the join model, and consequently it doesn't update the Section models modified column.

这篇关于保存多个在输入中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆