简体   繁体   中英

How to set sorting in default repository function in Typo3?

I have written like this

/**
 * itemRepository
 *
 * @var \KRT\KrtEmployee\Domain\Repository\ItemRepository
 * @inject
 */
 protected $itemRepository = null;

/**
 * action list
 *
 * @return void
 */
public function listAction()
{
    $arguments =$this->request->getArguments();     

    $employees = $this->itemRepository->findAll();        
    $this->view->assign('employees',$employees);
}

In my $employees result I have

  • Employee ID (Default uid)
  • Name
  • Designation
  • Department
  • Salary

Now, How can I Sort the result array in ascending order based on

  1. Name
  2. Department and Salary

Is there any default function to sort inside the repository queries?

Every repository has a $defaultOrderings property where you can specify the default orderings applied to all query methods. In your case it could look like this:

protected $defaultOrderings = [
    'name' => QueryInterface::ORDER_ASCENDING,
    'department.name' => QueryInterface::ORDER_ASCENDING,
    'salary' => QueryInterface::ORDER_ASCENDING,
];

As you can see with department.name you can also sort by properties of relations. Notice that this only works for 1:1 and n:1 relations.

In case of custom query methods in your repository you can manually set the orderings directly on the query:

$query->setOrderings([
    'name' => QueryInterface::ORDER_ASCENDING,
    'department.name' => QueryInterface::ORDER_ASCENDING,
    'salary' => QueryInterface::ORDER_ASCENDING,
]);

You have multiple options depending on what you would like to achieve:

Setting a default order for the entire repository

Add the following to your repository class

protected $defaultOrderings =
    array(
        'department' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
        'salary' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
    );

This will apply to all queries made in this repository.

see: https://wiki.typo3.org/Default_Orderings_and_Query_Settings_in_Repository

Setting an order for a single query

Add this to a query you define in your repository

$query->setOrderings(
    array(
        'department' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
        'salary' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
    )
);

In this way you could (and would have to) implement a different access method for each sort order you would like to have returned.

see: https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

Sort the result

You can always use PHP sorting methods to sort the query result (possibly converting it to an array with ->toArray() first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM