Issue filtering by attribute

So I am filtering by some “text” attributes without issue, but I am having trouble with one that is a number:

$pl->filterByAttribute(‘da’, $myNumber, “==”);

I am getting SQLSTATE[42000]: Syntax error or access violation: 1064… etc.
Do i have to do anything special with number attributes to avoid a SQL error?

Single = as third argument.

1 Like

Thank you very much!

Next Q, is it possible to filter with a range of values e.g.

$arrVals = [11, 12, 13];
$pl->filterByAttribute(‘da’, $arrVals, “=”);

I tried and it doesn’t seem to work.

You can reach underlying queryBuilder object for that.
For multiple OR alternatives, you can write something like:

$list = new \Concrete\Core\Page\PageList();

$qb = $list->getQueryObject();

$options = [
    'aaa',
    'bbb',
    'ccc',
];

$expressions = [];
foreach ($options as $k => $option) {
    $expressions[] = $qb->expr()->eq('ak_attribute_handle', ':value' . $k);
}

if (count($expressions)) {
    $qb->andWhere(
        $qb->expr()->or(...$expressions)
    );
    foreach ($options as $k => $option) {
        $qb->setParameter(':value' . $k, $option);
    }
}

//$list->debug();

$pages = $list->getResults();

Your attributes in sql query are accessible with ak_ prefix.
So if you are attribute is called color, then you have to replace ak_attribute_handle with ak_color.

You can uncomment $list->debug(); to display raw sql at the top of page.

You can read more about queryBuilder (and all of its methods) on:

2 Likes

Thanks so much for your help, that works well.