XF 2.1 IF NOT ISNULL with the XF2 finder system?

Jaxel

Well-known member
If I am fetching results from a table called packages, with the relation of colors, I could do the following:
Code:
$this->finder('EWR\Addon:Package')
    ->with(['Color'])
    ->fetch();

However, what if I wanted to do the equivalent of:
Code:
SELECT ...
    IF(NOT ISNULL(color.color_id), color.color_options, package.color_options) AS color_options

What would be the best way to do this?
 
PHP:
$package = $this->finder('EWR\Addon:Package')
    ->with(['Color'])
    ->fetch();
    
$colorOptions = $package->color_options ?: $package->Color->color_options;

Alternatively, you can create a getter on the Package entity that does the same as the last line there if you need to do this frequently.
 
Top Bottom