【问题标题】:Cakephp 2 issue with getting data in multiple secltionCakephp 2 在多项选择中获取数据的问题
【发布时间】:2017-04-25 06:51:40
【问题描述】:

我已将数据存储在两个表中的多选选项中。

表格: 用户:

id      user_name  status
1       test      1  
2       test2     1  
3       test3     1  
4       test4     1  

爱好:

id      hobby_name
1      Cricket        
2      Movie        
3      Football
4      Music
5      Dance   

我保存了如下数据:

id      user_id  hobby_id
1       1        5  
2       1        19  
3       2        17  
4       2        18  

每个用户都有多个爱好。现在,当我编辑 user_id 1 时,我想在多个选择框中显示选定的 ID。

我在UsersCroller.php 中提取了hobbies 数据,如下所示:

$this->loadModel('Hobby');
$hobbies = $this->Hobby->find('list', array('fields' => array('Hobby.id', 'Hobby.hobbyname'), 'recursive' => -1));
$this->set(compact('hobbies'));

admin_add.ctp 中创建了下拉选择框,如下所示:

<?php echo $this->Form->input('hobbies', array("label" => false, 'multiple' => 'multiple', 'type' => 'select', 'options' => $hobbies, "empty"=>"Select Hobbies", "div" => false, "class" => "m-wrap span12 validate[required]", "data-errormessage-value-missing" => "Please select the hobbies!" )); ?>

当我编辑和显示多选时如何获取这些记录。

【问题讨论】:

  • 你能在这里显示复选框吗?
  • 你试过'multiple' => true
  • 是的,我试过'multiple' =&gt; true
  • user_ids 是如何编辑的?我只是想弄清楚编辑用户 ID 和显示所选 ID 之间的关系是什么
  • 你在'options'中传递数组吗 => $options, 'selected' => $selected 这样

标签: php mysql cakephp cakephp-2.0 multiple-tables


【解决方案1】:

您可以尝试以下表单输入并传递选定的 ID 和所有爱好。

    $selected = array(5, 19);
    $options = array(5, 19, 17, 18);

    echo $this->Form->input('Hobbies', array('multiple' => true, 'options' => $options, 'selected' => $selected));

【讨论】:

  • 如何,我可以从admin_edit.ctp的数据库中获取选定的值。
  • 您可以使用 user_id 获取值,您将得到如下所示并在 $selected 数组 id user_id hobby_id 1 1 5 2 1 19 中创建带有 hobby_id 的数组
  • 艾哈迈德的答案是正确的。 $options 应该是查询所有爱好的结果,$selected 应该是属于用户的爱好列表。
【解决方案2】:
<?php
$results = $this->Users->find('all', array(
    'joins' => array(
        array(
            'table' => 'user_hobbies',
            'alias' => 'UserHobies',
            'type' => 'INNER',
            'conditions' => array(
                'UserHobies.id = User.id'
            )
        )
    ),
    'conditions' => array(
        'UserHobies.id' => 4   //  where user_id=4
    ),
    'fields' => array('Users.*,UserHobies.*'),
    ));

【讨论】:

  • 谢谢你的回答,我会试试上面的答案,会回复你的。
【解决方案3】:

如果您正确设置所有内容,CakePHP 将为您完成所有这些工作。

控制器

$user = $this->User->findById($id);
$this->request->data = $user;

// assigning the $user from the database with the associated HABTM
// to $this->request->data will auto-populate the input

$hobbies = $this->Hobby->find('list', array(
    'fields' => array('Hobby.id', 'Hobby.hobbyname')
));
$this->set(compact('hobbies'));

查看

echo $this->Form->inputs(array(
    'legend' => 'Hobbies',
    'Hobby.Hobby' => array(
        'label' => false,
            'type' => 'select',
            'multiple' => 'checkbox' // optional – I like checkboxes more
        )
    )
);

型号

public $hasAndBelongsToMany = array('Hobby');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多