0
votes

I've been searching through other tutorials and guides but haven't found anything that is doing what I'm after.

I have a form that has a multiple select field that returns an array. This array (Locationattribute) I'm looking to insert into another model referencing the associated field and Location. This is where I run into problems. I get the array from my form as below but in all the examples that I've found I need to include both the field and location_id with each data entry as I don't know how many

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
                    )

            )

    )

)

Is there any way to save this data in the format provided, if not how would I go about formatting my array to be acceptable format. I thought about just building it manually in my controller but I'm trying to adhere to best practices.

Note, I'm calling this save request from the Locations Model

For reference here are my relations

Location

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

1 Answers

0
votes

In your Location model, you are using LocationAttribute in your $hasMany declaration, but in the model, and the data, you have Locationattribute. Are you sure this isn't the cause of your problems?

I'd be curious to see what the schema of the Locationattribute model's table looks like. Are you trying to save one row in it for each of the 4 data array elements, or do they each correspond to a field in the table?