Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Model hasOne condition

Hi,

I have models that have various hasOne stated, however, those have soft delete columns and want to only return it if deleted is null.
Is there either a way of doing that through the definition?
Or is there a way I can do a condition somewhere else?

$this->hasOne('id', Test::class, 'testId', [
    'conditions' => 'deleted IS NULL',
    'alias' => 'test',
]);

$model->getTest(['conditions' => 'deleted IS NULL']);

Thanks,
Gary



114.4k
Accepted
answer

It should be:

[
    'params' => [
        'conditions' => 'deleted IS NULL',
    ],
    'alias' => 'test',
]

I tried that, but it didn't seem to do anything.

Ok, must have had a typo, worked perfectly that time.
Thanks.