shadowhand/latitude

Question: "WHERE IN" with Array

Spirit-act opened this issue · 1 comments

I open this issue in Refernce to #69

Maybe I'm missing something but I am unable to get this to work.

Pre-Informations:

The library is used for a project which needs to build querys dynamically.

I have a Helper Class which looks like this:

class Query
{
    /**
     * instantiates a new QueryFactory with the SQLServer Engine
     *
     * @return QueryFactory
     */
    public static function create() : QueryFactory
    {
        return new QueryFactory(new SqlServerEngine());
    }

    /**
     * instantiates a new SelectQuery
     *
     * @param string ...$columns (optional) Columns
     * @return SelectQuery
     */
    public static function select(string ...$columns) : SelectQuery
    {
        return self::create()->select(...$columns);
    }
}

First working test

$query = Query::select()
            ->from('user')
            ->where(field('fkuserworkflowstatus')->in(6, 7))
            ->orderBy('pkuser')
            ->offset(0)
            ->limit(10)
            ->compile();

Test with Array (not working)

$status = [6, 7];

$query = Query::select()
            ->from('user')
            ->where(field('fkuserworkflowstatus')->in($status))
            ->orderBy('pkuser')
            ->offset(0)
            ->limit(10)
            ->compile();

Workaround

$status = [6, 7];

$query = Query::select()
            ->from('user');

if (is_array($status) {
    $query = $query->where(call_user_func_array([field('fkuserworkflowstatus'), 'in'], $status));
}

$query = $query->orderBy('pkuser')
            ->offset(0)
            ->limit(10)
            ->compile();

This is a working solution but in my opinion not a good one.


I hope someone can show me the correct or a better way to do it.

Please read the comments of the issue you linked. I replied with the solution.