将数据动态添加到网格阵列

时间:2014-04-01 11:13:08

标签: php cakephp

我有一个数组,问题是我想在数组中添加一些字段,这对于保存操作的每个模型都是通用的。所以请建议我使用任何循环在运行时将字段值添加到数组中的适当解决方案。我的演示阵列是

Array
(
    [submit1] => Submit
    [Customer] => Array
        (
            [Code] => CUS-14-15-016
            [Name] => hggh
            [CreditLimit] => 
            [CreditDays] => 
            [Currency] => $
            [CreditTerm] => 
            [Category] => 
            [AmountDue] => 
            [SuspensionFrom] => Array
                (
                    [month] => 04
                    [day] => 01
                    [year] => 2014
                )

            [SuspensionTo] => Array
                (
                    [month] => 04
                    [day] => 01
                    [year] => 2014
                )

            [SuspensionReason] => 
        )

    [ContactAddress] => Array
        (
            [0] => Array
                (
                    [LineNo] => 0
                    [Deleted] => 0
                    [IsDirty] => 1
                    [Address] => gsdgs
                    [City] => gs
                    [State] => gfsd
                    [Country] => sdgsf
                    [ZipCode] => gsgd
                    [EmailId] => 
                    [Mobile] => 0
                    [Phone] => 0
                    [Fax] => 
                )

            [1] => Array
                (
                    [LineNo] => 0
                    [Deleted] => 0
                    [IsDirty] => 1
                    [Address] => sfvg
                    [City] => gfdgf
                    [State] => fdg
                    [Country] => dhgdh
                    [ZipCode] => 
                    [EmailId] => 
                    [Mobile] => 0
                    [Phone] => 0
                    [Fax] => 
                )

        )

)

我想补充一下 $ create = array(' CreatedBy' =>' 1',' ModifiedBy' =>'',' CreatedDate& #39; =>日期(' Ym-d'),' ModifiedDate' => date(' Ym-d')); 此字段值为Customer和Contact Address数组。

2 个答案:

答案 0 :(得分:0)

Cake会自动填充createdmodified等字段,因此您可以使用它并重命名数据库中的字段(如果可能)。唯一需要注意的是,在您的数据库中,我认为这些字段的类型为DATETIME

如果您想添加一些字段,可以使用model callbacks并将其放在AppModel中,或者如果您只有几个模型使用该动态创建的数据数组,则可以创建自定义行为。< / p>

答案 1 :(得分:0)

我不知道cakephp,但可能是

$demo_array['Customer']=array_merge($demo_array['Customer'],$create);
$demo_array['ContactAddress']=array_merge($demo_array['ContactAddress'], array('Modified'=>date('Y-m-d')));