Forums

Using hasMany

bmurphey 18 Feb, 2016
Trying to figure out how to show fields in a listing from an array returned using a hasMany relation. The array is returned as a separate Select. How do I go about showing those fields in the listing? I tried the model.field in the columns list, to no avail. Below is the output from Debugger with a pr($row):

[guests] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [uniq_id] => 362d68513585b533c43491cc48ed707d1eb0ae49
                    [user_id] => 486
                    [created] => 2016-02-16 19:17:46
                    [modified] => 
                    [p_id] => 29
                    [g_first_name] => 
                    [g_last_name] => 
                    [g_cell] => 567-989-6000
                    [g_email] => 
                )

            [1] => Array
                (
                    [id] => 4
                    [uniq_id] => 2c34bd2eee8a513a8959304a17b6165fa70795b2
                    [user_id] => 486
                    [created] => 2016-02-17 01:14:48
                    [modified] => 
                    [p_id] => 29
                    [g_first_name] => 
                    [g_last_name] => 
                    [g_cell] => 678-343-5567
                    [g_email] => 
                )

        )

)
GreyHead 18 Feb, 2016
Hi bmurphey,

I still find some parts of CC difficult to get my head round :-(

What do you want to show from this data? I might be able to work something out.

Bob
bmurphey 18 Feb, 2016
I would just like to show individual fields in the columns - basically the same as if I were putting 'model.field:Label' in the columns list. However, because the array generated from the hasMany relationship is a separate select, the columns list doesn't appear to recognize the model. Unfortunately my PHP skills are lacking, so I am not sure if there is a way to pull the fields out using the PHP functions.

Any ideas or suggestions you can provide would be greatly appreciated.

Thanks -

-Brian
bmurphey 18 Feb, 2016
Below is the full result of the pr($row), just for reference. (I deleted the names and phone numbers - those fields are not actually empty.)

I can list fields from the 'patient' model just fine, but not from the 'guests' model. The 'guests' model has a hasMany relationship to the 'patient' model (a patient can have many guests.) I am using the p_id in the 'guests' model as the foreign key (id in the 'patient' model is the primary key.)

Array
(
    [patient] => Array
        (
            [id] => 29
            [uniq_id] => 1de4969c74d6f279a78f476ff0f4a98fade003a8
            [user_id] => 486
            [created] => 2016-02-16 18:31:47
            [modified] => 
            [type] => 1
            [p_mrn] => 40320
            [p_first_name] => 
            [p_last_name] => 
            [p_address1] => 
            [p_address2] => 
            [p_city] => Hamilton
            [p_state] => OH
            [p_zip] => 45011
            [p_email] =>
            [p_email_notify] => No
            [p_cell] => 
            [p_cell_notify] => No
            [p_home] => 
            [p_notes] => Needs 1st floor room
            [start_date] => 2016-02-18
            [end_date] => 2016-03-25
            [room_id] => 52
            [state] => 0
            [calendar] => 0
        )

    [guests] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [uniq_id] => 362d68513585b533c43491cc48ed707d1eb0ae49
                    [user_id] => 486
                    [created] => 2016-02-16 19:17:46
                    [modified] => 
                    [p_id] => 29
                    [g_first_name] => 
                    [g_last_name] => 
                    [g_cell] => 
                    [g_email] => 
                )

            [1] => Array
                (
                    [id] => 4
                    [uniq_id] => 2c34bd2eee8a513a8959304a17b6165fa70795b2
                    [user_id] => 486
                    [created] => 2016-02-17 01:14:48
                    [modified] => 
                    [p_id] => 29
                    [g_first_name] => 
                    [g_last_name] =>
                    [g_cell] => 
                    [g_email] => 
                )

        )

)
GreyHead 18 Feb, 2016
Hi Brian,

I'm sorry I still don't understand what you want to show - there are multiple guest in each row so you can't just show the field values. You could perhaps build a list of guests like "name1, name2, . . ."

Bob
bmurphey 18 Feb, 2016
Correct. Column names are First Name, Last Name. Rows will be guest1, guest2, etc.
I have the patient name that these guests are associated with listed in the Header: "Guests of " patient name (you helped me figure that one out previously - some PHP in the header box). Columns list is set up as follows:

guests.g_first_name:First Name
guests.g_last_name:Last Name
_EDIT_:Edit
_DELETE_:Delete

I found that I can get the guests to list by reversing the models: Making the 'guests' model model#1, then making the 'patient' model the model#2 and having a 'belongsTo' relationship (guests belong to a patient). However, that prevents me from being able to automatically delete the guest records when I delete the patient record to whom they belong (the Associative Save and Associative Delete fields under Models only apply to model#2).

-Brian
bmurphey 18 Feb, 2016
OK, finally figured it out. I did not figure out a solution, but figured out what is going on so I can restructure everything. Just in case anyone else has the same confusion as I did about the 'hasMany' relationship:

The 'column list' generates rows based on the number of records from model#1 (1 row per record).
The 'hasMany' relationship is returning a single array that contains all records from model#2 that have a foreign key that matches the primary key of the model#1. So, for example, if model#1 is returning 1 record, and model#2 is returning 4 matching records, the $row data for the first row contains the array containing the one record from model#1 and the array containing the 4 records from model#2.

As far as I can tell, there doesn't appear to be any easy way to extract the individual records from the model#2 array within the $row such that each record can be displayed on separate rows.

So, I am switching back to a 'belongsTo' relationship, and handling the associative delete of the child model through a different listing.

I hope this helps someone.

Thanks -

-Brian
GreyHead 21 Feb, 2016
Hi Brian,

I think that you probably have got to the right answer here. CC is designed to produce a list based on the main table you select. You can add extra data to that list using linked tables but the rows are still going to be taken from the main table.

Bob
This topic is locked and no more replies can be posted.