arsis-dev
arsis-dev

Reputation: 453

Multiple Propel Like Filters

Trying to filter a column by multiple conditions using LIKE.

Like this:

$d = ItemQuery::create()
                ->filterByName($array_of_names, Criteria::LIKE)
                ->find();

But I get "Array to string conversion in propel/src/Propel/Runtime/Connection/StatementWrapper.php"

How can I filter by multiple "names" using the "LIKE" criteria?

I basically want the query to be

...name LIKE %name1% OR name LIKE %name2% OR name LIKE %name2%...

Upvotes: 1

Views: 1778

Answers (1)

Ben
Ben

Reputation: 713

Assuming that $array_of_names is [$name1, $name2, ...]

$q = ItemQuery::create()

foreach ($array_of_names as $i => $name) {
    if ($i > 0) { // Not the first item in the array
        $q->_or();
    }

    $q->where('Item.Name LIKE %?%', $name);
}

$d = $q->find();

See

http://propelorm.org/blog/2012/03/20/don-t-do-this-at-home-5-use-where-instead-of-filterby.html

and

http://propelorm.org/blog/2011/02/21/using-or-in-propel-queries-becomes-much-easier-with-propel-1-6.html

or

https://github.com/propelorm/Propel/issues/120

Upvotes: 1

Related Questions