We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



145.0k
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.