【问题标题】:How to link multiple records at once using cakephp hasMany?如何使用 cakephp hasMany 一次链接多个记录?
【发布时间】:2012-01-14 18:35:47
【问题描述】:

我用谷歌搜索了很多,但似乎没有什么与我正在寻找的相似..

我要做的是使用多个 HTML 列表一次将一条记录“用户”与其他多条记录“卡片”链接起来。

表格是: 用户(ID、姓名、用户名) 卡片(id,user_id)

型号:

class User extends AppModel {
    public $name = 'User';
    public $hasMany = array('Card');}

class Card extends AppModel {    
    public $name = 'Card';
    public $belongsTo = array('User');}

用户edit.ctp视图

echo $this->Form->input('id');
echo $this->Form->input('Card', array('multiple'=>true));

我的控制器会是什么样子?目前它看起来像这样,但只保存“没有相关卡”的用户记录

if (!empty($this->data)) {
    foreach($this->data['User']['Card'] as $key => $item){
        $this->data['User']['Card'][$key] = array('id'=>$item, 'user_id'=>$this->data['User']['id']);
                        }
        if ($this->User->saveAll($this->data)) {
            //$this->User->Card->saveAll($this->data);
            $this->Session->setFlash(__('The user has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
    }
}
if (empty($this->data)) {
    $this->data = $this->User->read(null, $id);
    $this->set('cards', $this->User->Card->find('list'));
}

$this->数据包含:

Array
(
    [User] => Array
    (
        [id] => 1
        [name] => Superman
        [status] => 1
        [Card] => Array
        (
            [0] => Array
            (
                [id] => 11402130001
                [user_id] => 1
            )
            [1] => Array
            (
                [id] => 11402130002
                [user_id] => 1
            )
            [2] => Array
            (
                [id] => 11402130003
                [user_id] => 1
            )
            [3] => Array
            (
                [id] => 11402130004
                [user_id] => 1
            )
        )
    )
)

【问题讨论】:

    标签: cakephp cakephp-1.3


    【解决方案1】:

    您需要为您的卡片添加一个 user_id 条目,以便您的数据看起来像这样:

    Array
    (
        [User] => Array
            (
                [id] => 10
            )
        [Card] => Array
            (
                [0] => Array
                    (
                    [id] => 1
                    [user_id] => 10
                    )
                [1] => Array
                    (
                    [id] => 2
                    [user_id] => 10
                    )
    
            )
    )
    

    我会在 $this->data['Card] 数组上进行 foreach 循环:

    foreach ($this->data['Card'] as &$card) {
      $card['user_id'] = $this->data['User']['id'];
    }
    

    然后您可以执行 saveAll。顺便说一句,应该删除 $this->User->Card->saveAll($this->data)

    【讨论】:

    • 感谢 G.J,但它不起作用!我以前试过这个,但它没有保存 user_id 知道有什么问题吗?我正在使用 1.3.4
    • @mmahgoub 如果将 Card 数组放在 User 数组之外会怎样?我不确定它是否会起作用。如果没有,也许您应该尝试通过保存卡片而不是用户来改变您的行为方式。告诉我它是否有效。
    • 嗨,G.J 实际上,如果它是这样的话,它确实可以工作:if ($this->User->saveAll($this->data['User'])) { $this->Session->setFlash(__('The user has been saved', true));} 但还有另一个问题! CakePHP 不识别未选择的项目,换句话说,当我选择它们时它可以将卡片链接到用户,但当我删除选择时它不会取消链接!知道如何在没有 foreach 循环和 saveField 和这类东西的情况下实现这一目标吗?
    • 我想我只需要将 user_id 设置为 0 但我如何才能实现我只在 Card 数组中获取选定的项目?!
    • 这似乎不可能,因为 $_POST 它本身不包含未选中的项目,因此必须通过 for 循环进行比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2016-08-12
    相关资源
    最近更新 更多