CakePHP:保存多个复选框关联数据

时间:2014-07-25 00:14:10

标签: php arrays cakephp cakephp-2.3

我一直在搜索其他教程和指南,但没有发现任何正在做我想要的事情。

我有一个具有多个选择字段的表单,该字段返回一个数组。这个数组(Locationattribute)我想插入引用相关字段和Location的另一个模型。 这是我遇到问题的地方。我从我的表单中获取数组,如下所示,但在我发现的所有示例中,我需要在每个数据条目中包含字段和location_id,因为我不知道有多少

Array

[Location] => Array
    (
        [name] => 
        [type] => 
        [specialities] => 
        [phone] => 
        [address_num] => 
        [address_street] => 
        [address_suffix] => 
        [address_state] => 
        [address_suburb] => 
        [address_postcode] => 
        [num_docs] => 
        [additional_services] => 
        [funds] => 
        [created_by] => 1
        [assigned_to] => 1
    )

[Hour] => Array
    (
        [mon_active] => 0
        [mon_open] => 10:00
        [mon_close] => 10:00
        [tues_active] => 0
        [tues_open] => 10:00
        [tues_close] => 10:00
        [wed_active] => 0
        [wed_open] => 10:00
        [wed_close] => 10:00
        [thurs_active] => 0
        [thurs_open] => 10:00
        [thurs_close] => 10:00
        [fri_active] => 0
        [fri_open] => 10:00
        [fri_close] => 10:00
        [sat_active] => 0
        [sat_open] => 10:00
        [sat_close] => 10:00
        [sun_active] => 0
        [sun_open] => 10:00
        [sun_close] => 10:00
    )

[Locationattibute] => Array
    (
        [1] => Array
            (
                [field] => patient_type
                [created_by] => 1
                [assigned_to] => 1
                [data] => Array
                    (
                        [0] => Individuals
                        [1] => Families
                        [2] => Children
                        [3] => Elderly
                    )

            )

    )

有没有办法以提供的格式保存这些数据,如果不是,我将如何将我的数组格式化为可接受的格式。我想过只是在我的控制器中手动构建它,但我正在努力坚持最佳实践。

注意,我正在从地点模型中调用此保存请求

以下是我的关系

位置

class Location extends AppModel {

public $hasMany = array('Contact', 'Response', 'LocationAttribute');

public $hasOne = array('Hour');

public $belongsTo = array('User' => array(
    'className'     => 'User',
    'foreignKey'    => 'assigned_to',
    'dependent'     => false
));

Locationattribute

class Locationattribute extends AppModel {

public $belongsTo = array('Location' => array(
    'className'     => 'Location',
    'foreignKey'    => 'location_id',
    'dependent'     => false
));

1 个答案:

答案 0 :(得分:0)

在您的位置模型中,您在LocationAttribute声明中使用$hasMany,但在模型和数据中,您有Locationattribute。你确定这不是你问题的原因吗?

我很想知道Locationattribute模型表的模式是什么样的。您是否尝试为4个数据数组元素中的每一个保存一行,或者它们是否都对应于表中的字段?