Saturday, March 19, 2022

[FIXED] CakePHP retrieving when related model NULL

Issue

I have a null-able relation model, and I am using this standard code to retrieve my records:

$this->paginate = [
    'contain' => ['Clients']
];
$coupons = $this->paginate($this->Coupons);

I am getting just the records that have client associated. what is the best practice to make the contain work like OR, and not AND

EDIT: the relationship is seted as the following:

$this->belongsTo('Clients', [
    'foreignKey' => 'client_id',
    'joinType' => 'INNER'
]);

Solution

I have solved it by setting the joinType to LEFT:

$this->belongsTo('Clients', [
    'foreignKey' => 'client_id',
    'joinType' => 'LEFT'
]);


Answered By - Eymen Elkum

No comments:

Post a Comment

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