1

我在 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->data 看起来像这样:

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
            )

    )

)

现在,由于与此模型(警报、ForecastZone 和区域)的复杂关联,我已经对其进行了设置,因此警报/编辑页面上有一个单独的界面来更新预测区域。警报控制器计算出预测区域所属的区域,并且控制器自己填充 [Region][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->Alert->save($this->data) 以及 $this->Alert->saveAll($this->data) 并且这些都不起作用。两个 save() 函数都返回 1(真),所以我认为它们是成功的......但是当我返回到我最近编辑的记录时......什么都没有改变。我尝试将所有其他警报数据作为隐藏字段添加到表单中,这样我就可以一次保存整个警报数据......这也不起作用。

编辑:

这是我的 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());              

    } 

}

最好的错误是那些不会引发错误或有任何方法让您找出问题所在的错误。如此愤怒和沮丧......请帮忙。

4

2 回答 2

0

玩弄数据的键控。

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

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

于 2012-08-15T23:32:54.967 回答
0

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

于 2016-10-18T16:02:28.237 回答