Association Issue

I’m having problems with an association CakePHP 2.9.1, I wonder if someone should point me in the right direction, the code is running from a plugin, the data is selected but the array returned only contains data for the current model “OnCallteam”, the other model is there in the array but it’s empty and a warning is issued to the console.

Model “Oncallteam” is not associated with model “Oncallteam” in [/usr/share/XXXXX/lib/Cake/Model/Behavior/ContainableBehavior.php,

I don’t know why I’m being issued this warning because the model I’m calling from is Oncallteam so don’t understand why it’s having an issue calling itself. (I’ve had to change the naming to remove the company name from the post)

Here is my model (I’ve removed
class Oncallteam extends SchedulerModuleAppModel
{

    // Oncall structure ----> Group --> Team --> booking

    public $useTable = 'oncallteams';

    public $hasAndBelongsToMany = [
        'Tenant' => [
            'className' => 'Tenant',
            'joinTable' => 'oncallteams_to_tenants',
            'unique' => true,
            'dependent' => false, // We don't want to delete the Service Template by mistake!
        ]

    ];


    public $hasMany = [
        'Oncallbooking' => [
            'className' => 'SchedulerModule.GeOncallbooking',
            'foreignKey' => 'oncall_team_id',
            'dependent' => true,
        ],
    ];

    public $belongsTo = [
        'GeOncallgroup' => [
            'className' => 'SchedulerModule.GeOncallgroup',
            'foreignKey' => 'oncall_group_id'
        ]
    ];

Here is the function, it’s inside this model, I want to get engineers with a booking before 26 April 2017 (in model Oncallbooking), and a team id of 25 (in this model):

 $result = $this->find("all", [
                'contain' => [
                    'Oncallbooking' => [
                        'conditions' => [
                            'Oncallbooking.from' => '< 2017-04-26'
                        ],
                        'fields' => [
                            'Oncallbooking.*'
                        ],
                    ],

                    'Oncallteam' => [
                        'conditions' => [
                            'Oncallteam.id' => 25
                        ]
                    ],
                ],

                'fields' => [
                    'Oncallteam.*',
                ],

            ]);

Thanks in advance for any help and sorry if I’ve done something stupid.

You need to post code for OnCallBooking model associations. I think the issue could be there

Many thanks for the reply, here is the code for OnCallBooking

class GeOncallbooking extends GeneralelectricModuleAppModel
{

    // Oncall structure ----> Group --> Team --> booking
    public $useTable = 'oncallbookings';

    public $belongsTo = [
        'User' => [
            'className' => 'User',
            'foreignKey' => 'user_id'
        ],
        'Oncallteam' => [
            'className' => 'SchedulerModule.Oncallteam',
            'foreignKey' => 'oncall_team_id'
        ]
    ];
}

Ok! So the associations in your GeOnCallBooking looks fine. So going back to your OncallTeam model, your are ‘containing’ the Oncallteam within itself.

'Oncallteam' => [
    'conditions' => [
        'Oncallteam.id' => 25
    ]
],

It should only be added as a condition outside of the contain array. Try that out!

1 Like
'conditions' => [
    'Oncallteam.id' => 25
]
1 Like

Ok, so it doesn’t complain now and returns 1 row instead of loads, but Oncallbooking is an empty array, if I add ‘Oncallbooking.*’ to the main fields it errors.

Array
(
    [0] => Array
        (
            [Oncallteam] => Array
                (
                    [id] => 25
                    [oncall_group_id] => 6
                    [name] => Group1
                    [comments] => 
                    [sms_from] => +4915222222
                    [product_group] => Group
                )

            [Oncallbooking] => Array
                (
                )

        )

)

Found the remaining issue, it was because, I had the logic operator < inside the value not the column so it was building an SQL with Oncallbooking.from = ‘< 2017-04-26’, which MySQL was taking as a literal string so it was never matching anything.

'conditions' => [
                            'Oncallbooking.from' => '< 2017-04-26'
                        ],

should have been:

    'conditions' => [
                                'Oncallbooking.from <' => '2017-04-26'
                            ],

Many thanks for your help.