简体   繁体   English

CakePHP需要模型索引数组,但不能从for循环计数器生成。 需要模型索引数组才能工作

[英]CakePHP Model index array needed, but can't generate from for loop counter. Need model indexed array for this to work

I am working on a function that submits multiple records on various relationship types. 我正在使用一个功能,该功能针对各种关系类型提交多个记录。 The main issue I am running into is the format of the array. 我遇到的主要问题是数组的格式。 In order for my saveAll() to work on my multiple relationships setup, the array needs to be in this format as you can see the models are Keys (first array below). 为了使我的saveAll()可以在我的多关系设置上工作,数组必须采用这种格式,因为您可以看到模型是Keys(下面的第一个数组)。

My main question is: 1) Is it possible to strip the numerical indexes off the second layer of the second array below? 我的主要问题是:1)是否可以从下面的第二个数组的第二层剥离数字索引?

I am returning my input fields like so. 我将像这样返回我的输入字段。 You can see the prefixed counter (which I believe is what is creating the numeric index on that second level). 您可以看到带前缀的计数器(我相信这是在第二级上创建数字索引的内容)。

<?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?>

I am using a for loop counter for the fields. 我正在为字段使用for循环计数器。 So my question number to is: can this for value be changed to something that will work with Cake's saveAll()? 因此,我要问的问题是:是否可以将此取值更改为与Cake的saveAll()一起使用的值?

<?php for ($i = 1; $i <= 2; $i++) { ?>

Example where models are the keys (this is the format I need): 模型是关键的示例(这是我需要的格式):

Array
(
[User] => Array
    (
        [username] => billy
    )
[Profile] => Array
    (
        [sex] => Male
    [occupation] => Programmer
    )

The only output I can get on my multiple input array (below is the debug() dump) 我可以在多个输入数组上获得的唯一输出(下面是debug()转储)

My actual output is numerically indexed: 我的实际输出数字索引:

Array
(
[Plan] => Array
    (
        [1] => Array
            (
                [plan_detail_id] => 36
                [monthly_cost] => 0
                [dental_cost] => 0
                [age_id] => 14
                [applicant_id] => 1
                [state_id] => 1
            )

        [2] => Array
            (
                [plan_detail_id] => 36
                [monthly_cost] => 0
                [dental_cost] => 0
                [age_id] => 2
                [applicant_id] => 4
                [state_id] => 1
            )

    )

[1] => Array
    (
        [1] => Array
            (
                [Zip] => Array
                    (
                        [0] => 487
                        [1] => 486
                        [2] => 485
                        [3] => 484
                        [4] => 483
                    )

            )

    )

[2] => Array
    (
        [2] => Array
            (
                [Zip] => Array
                    (
                        [0] => 485
                        [1] => 484
                        [2] => 483
                    )

            )

    )

)

Did you already check out the Set Core Utility Library ? 您是否已经签出了Set Core Utility Library This can help you out a lot with array management. 这可以对阵列管理有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM