[TYPO3-mvc] How to set the sort order for child objects (m-m relation)
Bernhard Esders
be2010 at ueppste.de
Thu Aug 4 21:11:31 CEST 2011
I solved this problem with a view helper.
Not nice, but working.
class Tx_Myextension_ViewHelpers_SortViewHelper extends
Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
private $orderBy;
private $order;
/**
* Sorts the given elements
*
* @param Tx_Extbase_Persistence_ObjectStorage $objects
* @param string $orderBy
* @param string $order
* @return Tx_Extbase_Persistence_ObjectStorage sortet Objects
* @api
*/
public function render(Tx_Extbase_Persistence_ObjectStorage $objects,
$orderBy=NULL, $order=NULL) {
$this->orderBy = $orderBy;
$this->order = $order;
$sort_objects = $this->sortObjectStorage($objects);
return $sort_objects;
}
protected function sortObjectStorage($storage) {
$sorted = array();
foreach ($storage as $item) {
$sorted_array[] = $item;
}
usort($sorted_array,array($this,compare));
$sorted_storage =
$this->objectManager->get('Tx_Extbase_Persistence_ObjectStorage');
foreach ($sorted_array as $item) {
$sorted_storage->attach($item);
}
return $sorted_storage;
}
protected function compare($object1, $object2){
if ($this->getSortValue($object1) == $this->getSortValue($object2)) {
return 0;
}
if ($this->order === 'ASC')
return ($this->getSortValue($object1) <
$this->getSortValue($object2)) ? -1 : 1;
return ($this->getSortValue($object1) > $this->getSortValue($object2))
? -1 : 1;
}
protected function getSortValue($object) {
if ($this->orderBy) {
$getter = 'get' . ucfirst($this->orderBy);
} else {
$getter = "getUid";
}
if (method_exists($object, $getter)) {
$value = $object->$getter();
} else if (is_object ($object)) {
$value = $object->$field;
} else if (is_array($object)) {
$value = $object[$field];
}
if ($value instanceof DateTime) {
$value = $value->getTimestamp();
}
return $value;
}
}
Am 04.08.2011 17:33, schrieb g4-lisz at tonarchiv.ch:
> On 08/04/2011 04:30 PM, Werner Stotter wrote:
>> hi,
>>
>> have you tried setting the 'sorting' in the TCA of ext_tables.php?
>>
>> in my case this helped
>>
>> greetings
>> werner
>>
>>
> Hi Werner
>
> Sadly this did not solve my problem.
>
> The issue is that the ordering property belongs to the next but one
> table. In SQL this would be a SORTY BY a row of a secondary joined table:
>
> SELECT
> h.name AS hospital_name
> ,d.name AS disease_name
> ,t.number AS disease_number
> FROM
> hospital h
> JOIN
> treatment t
> ON
> h.treatment=t.uid
> JOIN
> disease d
> ON
> t.disease=d.uid
> ORDER BY
> d.name
> ;
>
> A 'foreign_table_where' => 'ORDER BY disease.name' statement in the
> treatment table's TCA makes no sense because there is a 1:1 relation
> between desease and treatment. In the hospital table's TCA i don't know
> how to set the ordering by a property that belongs to the next but one
> table...
>
> I think i have to set a SQL statement by my own, but at which place do i
> define this $query?
>
> Thank you,
> Till
>
>>
>>
>> Am 04.08.2011 15:44, schrieb g4-lisz at tonarchiv.ch:
>>> On 30.07.2011 03:57, g4-lisz at tonarchiv.ch wrote:
>>>> Hello all
>>>>
>>>> I'm writing an extension for the following domain (simplified):
>>>>
>>>> Entities: Hospitals, treatments
>>>> Value Objects: disease
>>>>
>>>> Every hospital has several treatments. Every treatment has one disease.
>>>>
>>>> Hospital is "aggregate root".
>>>>
>>>> A simple view which shows all treatments for a particular hospital
>>>> could look like this:
>>>>
>>>> <f:for each="{hospital.treatments}" as="treatment">
>>>> Disease: {treatment.disease.name} Treatments: {treatment.number}
>>>> </f:for>
>>>>
>>>> I'd like to sort the results list by disease names.
>>>>
>>>> How can i achieve this?
>>>>
>>>> I know there's the possibility to set orderings for a query obejct.
>>>>
>>>> But in my case there's only one Repository for the aggregate root,
>>>> namely "Hospitals". How to set orderings for subsidiary entities?
>>>>
>>>> Greetings,
>>>> T
>>> Really no one any idea?
>>>
>>> I think this is a quit common problem and there must be a common
>>> solution?
>>>
>>> Any help will be much appreciated!
>>
>> _______________________________________________
>> TYPO3-project-typo3v4mvc mailing list
>> TYPO3-project-typo3v4mvc at lists.typo3.org
>> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
>
More information about the TYPO3-project-typo3v4mvc
mailing list