English 中文(简体)
为什么全部保存只保存最后记录?
原标题:Why is saveAll() only saving the last record?

我有一种情况,由于外部原因,我必须直接保存合并记录,而不是作为合并的一部分保存记录。

  • I have a Firm model whose data is pulled from an external source.
  • I have a County model in my app database
  • I have a counties_firms join table that I use to associate those external firms to counties.

由于生活所在,我不编辑 Firm 模型,我也不编辑 County 模型。我其实只是编辑协会。我有 Firm 模型,可以包罗我需要与公司数据有关的一切,其中一种方法是 Firm:saveCounty($data) 。它:

  • Accepts incoming data that includes the firm id and the county ids that should be associated.
  • Deletes all existing join records for that county
  • Attempts to save all of the new join records.

我发现只有最后的县级记录被保存了

Array
(
    [0] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e24-a25c-4523-8a9e-7d1d147402e8
        )

    [1] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e27-ccd0-4f22-97da-7d1d147402e8
        )

    [2] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e4a-68f8-4fb1-95bb-7d1d147402e8
        )

)

鉴于上述数据,我将在 Firm CountysFirm 之间建立实时联系,并尝试 $this-gt;CountysFirm-gt;saveall($data)

正如我提到的那样,在这个例子中,只有3个县协会中的最后一家得到拯救。 知道我会错过什么吗?

谢谢

最佳回答

我相信你在你的阵列里漏掉了一个水平... 它应该看起来更像这个...

Array(
     CountiesFirm  => array(
        [0] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e24-a25c-4523-8a9e-7d1d147402e8
        )
        [1] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e27-ccd0-4f22-97da-7d1d147402e8
        )
        [2] => Array
        (
            [firm_id] => 13
            [county_id] => 4fa16e4a-68f8-4fb1-95bb-7d1d147402e8
        )
    )
)

试试试试,让我知道你的结果

问题回答

您的阵列是罚款的。 模式似乎没有清除其代号, 您可以尝试添加 [ id] \\\\ gt; 无效到您阵列中的每个记录, 以强制模式 ID 清除 。

这对我管用





相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

Using DISTINCT in a CakePHP find function

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

Prevent controller from trying to autoload model

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try to call ...

热门标签