Skip to content

Instantly share code, notes, and snippets.

@Digi92
Created January 29, 2019 11:07
Show Gist options
  • Save Digi92/c14e32d35636dc9e042b77ac829b001f to your computer and use it in GitHub Desktop.
Save Digi92/c14e32d35636dc9e042b77ac829b001f to your computer and use it in GitHub Desktop.
Get the Products in the order of the uid comma list in TYPO3 8 Source: https://www.derhansen.de/2017/05/mysql-field-function-in-typo3-87-with-doctrine.html
<?php
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
/**
* The repository for ExampleRepository
*/
class ExampleRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
/**
* Find all results by a list of uids and order by this uid list
*
* @param String $uidList List of uids
* @return array
*/
public function findAllByCommaIdString(String $uidList) {
$uids = GeneralUtility::intExplode(',', $uidList, true);
if ($uidList === '' || count($uids) === 0) {
return [];
}
$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_example_domain_model_example');
$rows = $queryBuilder
->select('*')
->from('tx_example_domain_model_example')
->where($queryBuilder->expr()->in('uid', $uids))
->add(
'orderBy',
'FIELD(tx_example_domain_model_example.uid,' . implode(',', $uids) . ')'
)
->execute()
->fetchAll();
return $dataMapper->map(\FooBar\Example\Domain\Model\Example::class, $rows);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment