0

I have build an extension with the extension builder that handles objects, an object can be an "item" or a "project".

An object has a field status which has 4 options, and is filled with an integer (3 = sold).

An object can be signed as a "project", in that case it has a boolean 1 for isproject and a field items with related objects as items.

this all works fine, I can iterate trough the projects with my fluid template and with <f:count>{object.items}</f:count> display the number of items appartaining to the project.

In the same fashion I should display the count of only the sold items ...
(<f:count where="object.items.status == 3">{object.items}</f:count> this obviously does not work, just to render the idea)

with <f:debug>{object}</f:debug> I see the field status defined for all items ...
since I have no idea how to approch this I might have left out some vital information

webman
  • 1,117
  • 15
  • 41

3 Answers3

1

As indicated in the previous answer, you can use the GroupedFor ViewHelper for this purpose. But using it would be to much logic inside the fluid template thats the reason why this should be done in the controller, model or repository.

Example: Adding a Getter to the Object-Model

/**
 * @return int
 */
public function getSoldItems() {
    $soldCount = 0;
    foreach($this->getItems() as $item) {
        // 0 = sold
        if($item->getStatus()===0) {
            $soldCount++;
        }
    }
    return $soldCount;
}

In fluid you can call the Getter with {object.soldItems}

A better performance solution especially with lazy loading subobjects would be counting with the repository. For this you have to create a function in the repository and call it inside the Getter-Function. To use common repository methods for creating the query, you need a "Backrelation" of items to the object. Otherwise you have to write the query with "statement()" on your own.

Paul Beck
  • 2,675
  • 1
  • 12
  • 14
0

You can count them in the controller or use the GroupedFor ViewHelper https://fluidtypo3.org/viewhelpers/fluid/master/GroupedForViewHelper.html

Paul Beck
  • 2,675
  • 1
  • 12
  • 14
  • @Paul_Beck, counting in the controller sounds good, how do you go about for that ... the GroupedFor seems to provide an array, that's not really needed, I have the `object` with field `items` with field `status` in turn, that should be counted and displayed in fluid ... schwerin !! – webman Jan 23 '17 at 12:33
  • With the GroupedFor viewhelper you can sort your childs and then count them by status (count the array). But this would be almost to much logic for fluid and should belong inside the controller. In the controller you have full power of PHP or SQL inside the repository to count the items. – Paul Beck Jan 23 '17 at 12:42
  • I am a beginner, I have no clue how to take your info a step further ... you know of some codede example ? – webman Jan 23 '17 at 12:51
  • As described on Skype some mins ago. Have fun :-) – Paul Beck Jan 23 '17 at 17:17
  • Paul, for good practice would you like to add the solution in the object model as answer so I can sign it as accepted answer ... otherwise I'll supply it myself – webman Jan 24 '17 at 03:22
0

Check this workaround:

<f:variable name="counter" value="0" />

<!-- Loop over the elements -->
<f:for each="{bookings}" as="booking">
  <!-- Check if the criteria is met -->
  <f:if condition="{booking.paid}">
    <!-- Increment the counter -->
    <f:variable name="counter" value="{counter + 1}" />
  </f:if>
</f:for>
<!-- Display the counter-Variable -->
{counter}
  • homepage helden ?? I accepted paul becks answer years ago ... don't even recall what the trouble was – webman Feb 08 '23 at 15:24
  • Yes... but... You, are not the only one reading this and looking for the answer to the same problem... so was I looking, tried also the here suggested answers, came up with a working solution... and thought I'd share it... – Richard Albrecht Feb 09 '23 at 18:21