CakePHP 1.3 HABTM - 添加工作正常,编辑不会保存HABTM

时间:2012-07-05 15:56:39

标签: cakephp save edit has-and-belongs-to-many

我在CakePHP 1.3中遇到HABTM更新问题。

首先,这是警报模型中出现的关系声明:

var $hasAndBelongsToMany = array(
    'Region' => array(
        'className' => 'Region',
        'joinTable' => 'alerts_regions',
        'foreignKey' => 'alert_id',
        'associationForeignKey' => 'region_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'ForecastZone' => array(
        'className' => 'ForecastZone',
        'joinTable' => 'alerts_forecast_zones',
        'foreignKey' => 'alert_id',
        'associationForeignKey' => 'forecast_zone_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )       
);

当我提交表单为模型创建记录时,一切正常,相关模型(HABTM表)可以创建正确的记录。 $ this->数据如下所示:

Array
(
[Alert] => Array
    (
        [title] => Test Alert
        [overview] => This is a test alert overview
        [user_id] => 3
        [timeframe] => Tonight
        [effective] => 2012-07-05 18:00:00
        [effective_time] => 6:00
        [effective_ampm] => pm
        [expires] => 2012-07-05 21:00:00
        [expires_time] => 9:00
        [expires_ampm] => pm
    )

[Region] => Array
    (
        [Region] => Array
            (
                [0] => 6
                [1] => 5
            )

    )

[ForecastZone] => Array
    (
        [ForecastZone] => Array
            (
                [0] => 1598
                [1] => 1594
                [2] => 2685
                [3] => 1565
                [4] => 1613
                [5] => 1595
            )

    )

)

现在,由于与此模型(Alert,ForecastZone和Region)的复杂关联,我设置了它,因此警报/编辑页面上有一个单独的界面来更新预测区域。警报控制器指出预测区域所属的区域,控制器自己填充[区域] [区域]值。 ForecastZones是从用户单击的界面中选择的,以选择他们想要的区域。

当我向/ alerts / edit操作提交表单时,我可以保存警报数据(标题,概述等)没问题。但是,让我更新相关的HABTM模型是不可能的。我正在使用$ this-> query()完成所有操作,但我将最后一次尝试找出问题所在。

以下是我提交/ alerts / edit时表单的样子:

Array
(
    [Alert] => Array
        (
            [id] => 37
            [fcz_update] => true
        )

    [Region] => Array
        (
            [Region] => Array
                (
                    [0] => 6
                    [1] => 5
                )

        )

    [ForecastZone] => Array
        (
            [ForecastZone] => Array
                (
                    [0] => 1595
                    [1] => 1613
                    [2] => 1565
                    [3] => 2685
                    [4] => 1594
                    [5] => 1598
                    [6] => 2989
                    [7] => 3723
                    [8] => 1650
                    [9] => 1626
                    [10] => 1653
                    [11] => 1678
                    [12] => 3447
                    [13] => 1649
                    [14] => 1654
                    [15] => 1675
                    [16] => 3448
                    [17] => 1668
                    [18] => 1664
                    [19] => 1635
                    [20] => 1667
                    [21] => 1628
                )

        )

)

我已经尝试了$ this->提醒 - >保存($ this->数据)以及$ this->提醒 - > saveAll($ this->数据)这些都不起作用。 save()函数都返回1(true)所以我认为它们是成功的......但是当我返回到我最近编辑的记录时......没有任何改变。我尝试将所有其他警报数据添加到表单中作为隐藏字段,因此我可以立即保存整个警报数据......这也不起作用。

编辑:

以下是我的alerts_controller.php编辑功能/操作中执行保存操作的代码:

if (!empty($this->data)) {

    //If we are updating forecast zones
    if(array_key_exists('fcz_update',$this->data['Alert'])){ 

        $fcz_str = $this->data['Alert']['AlertForecastZone'];
        $forecast_zones = explode(',',$fcz_str);

        $fcz_regions = $this->ForecastZone->query('SELECT DISTINCT region_id FROM forecast_zones WHERE id IN (' . $fcz_str . ') AND region_id != 0;');


        if(!empty($fcz_regions)){

            foreach($fcz_regions as $fczr){ $alert_regions[] = $fczr['forecast_zones']['region_id']; }
            $this->data['Region']['Region'] = $alert_regions;   

        }


        $this->data['ForecastZone']['ForecastZone'] = $forecast_zones;
        unset($this->data['Alert']['AlertForecastZone']);               

        //debug($this->data);

        $this->Alert->save($this->data);

        //Go back to alert edit to review changes
        $this->redirect($this->referer());              

    } 

}

最好的错误是没有错误或有任何方法让你弄清楚出了什么问题。如此愤怒和沮丧...请帮助。

2 个答案:

答案 0 :(得分:0)

使用键入数据进行游戏。

尝试[ForecastZone] [0] => 1595 要么 [ForecastZone] [0] [id] => 1595 甚至 [ForecastZone] [0] [ForecastZone] => 1595

我总是要努力记住HABTM正确的键控顺序

答案 1 :(得分:0)

要保存关联数据和相关数据,您需要使用saveAll函数$this->Alert->saveAll($this->data)