# Association Issue

**URL:** <https://discourse.cakephp.org/t/association-issue/2491>\
**Category:** Need Help\
**Created:** [April 27, 2017, 2:03pm UTC](https://discourse.cakephp.org/t/association-issue/2491 "2017-04-27T14:03:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevrobwhite](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@trevrobwhite](https://discourse.cakephp.org/u/trevrobwhite)\
**Post date:** [April 27, 2017, 2:03pm UTC](https://discourse.cakephp.org/t/association-issue/2491/1 "2017-04-27T14:03:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![akkaweb](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/akkaweb/32/53_2.png) [@akkaweb](https://discourse.cakephp.org/u/akkaweb)\
**Post date:** [April 27, 2017, 3:45pm UTC](https://discourse.cakephp.org/t/association-issue/2491/2 "2017-04-27T15:45:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trevrobwhite](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@trevrobwhite](https://discourse.cakephp.org/u/trevrobwhite)\
**Post date:** [April 27, 2017, 3:58pm UTC](https://discourse.cakephp.org/t/association-issue/2491/3 "2017-04-27T15:58:57Z")

</div>

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'
        ]
    ];
}
```

---

<div class="post-metadata">

**Author:** ![akkaweb](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/akkaweb/32/53_2.png) [@akkaweb](https://discourse.cakephp.org/u/akkaweb)\
**Post date:** [April 27, 2017, 4:05pm UTC](https://discourse.cakephp.org/t/association-issue/2491/4 "2017-04-27T16:05:00Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![akkaweb](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/akkaweb/32/53_2.png) [@akkaweb](https://discourse.cakephp.org/u/akkaweb)\
**Post date:** [April 27, 2017, 4:06pm UTC](https://discourse.cakephp.org/t/association-issue/2491/5 "2017-04-27T16:06:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trevrobwhite](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@trevrobwhite](https://discourse.cakephp.org/u/trevrobwhite)\
**Post date:** [April 27, 2017, 4:27pm UTC](https://discourse.cakephp.org/t/association-issue/2491/6 "2017-04-27T16:27:23Z")

</div>

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
                (
                )

        )

)
```

---

<div class="post-metadata">

**Author:** ![trevrobwhite](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@trevrobwhite](https://discourse.cakephp.org/u/trevrobwhite)\
**Post date:** [April 27, 2017, 6:17pm UTC](https://discourse.cakephp.org/t/association-issue/2491/7 "2017-04-27T18:17:46Z")

</div>

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.
