Wednesday, January 5, 2022

[FIXED] Get related data in contain request

Issue

Consider these (CakePHP3) models and their relations:

Menus

name | user_id

Menutimetables

herd_id | menu_id | start | end

Menus hasMany Menutimetables

Menutimetables belongsTo Menus

Menutimetables belongsTo Herds

Herds hasMany Menutimetables

In my controller I'm getting all data with

$herd = $this->Herds->get($id, [
    'contain' => [
        'Menutimetables' => ['sort' => ['start'=>'asc']]
    ]
]);

Which results in getting the menu_id.

menu_id   start   end
8         1       5
9         6       9
6         10      15

Where in fact I want to see the Menus.name.

How can I get that?


Solution

Try:

$herd = $this->Herds->get($id, [
    'contain' => [
        'Menutimetables.Menus'
        'Menutimetables' => ['sort' => ['start'=>'asc']]
    ]
]);


Answered By - Salines

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.