Ted Logan
Ted Logan

Reputation: 414

TYPO3 v8 QueryBuilder WHERE IN statement

Im trying to use a WHERE INstatement for the QueryBuilder. I want to call

UPDATE tx_test SET hidden = 0 WHERE uid IN (1,2,3,4) 

my code:

    public function makeItemsVisible($itemsToShow)
    {
        $itemUids = implode(",", $itemsToShow);
        $table = 'tx_test';
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable($table);
        $queryBuilder
            ->update($table)
            ->set('hidden', "0")
            ->where('uid IN (:uids)')
            ->setParameter('uids', $itemUids )
            ->execute();
    }

The $itemUids 1,2,3,4 are correct, but the statement is only updating the first record in the Database. Do I missed something?

Upvotes: 1

Views: 1657

Answers (1)

Mathias Brodala
Mathias Brodala

Reputation: 6460

You should use the expression builder and the API to add named parameters:

$queryBuilder
  ->update($table)
  ->set('hidden', 0)
  ->where($queryBuilder->expr()->in(
      'uid',
      $queryBuilder->createNamedParameter($itemUids, Connection::PARAM_INT_ARRAY)
  )
  ->execute();

See the Comparisons section in the ExpressionBuilder documentation for details.

Upvotes: 2

Related Questions