Extbase add calculated field in query/repository

653 views Asked by At

I have a normal extbase extension.

In the controller I get all my records:

$stuff = $this->jobRepository->findAll();
$this->view->assign('stuff', $stuff);

and in the template I display them:

<f:for each="{stuffs}" as="stuff">
{stuff.title} <br />
{stuff.category}
</f:for>

Now I need a new field stuff.isnew with the value 1 if the record is the newest by category.

An SQL-Statement for that would look like:

SELECT  j2.isnew, j.* FROM `tx_stuff_domain_model_stuff` as j
left join 
    (SELECT  max(crdate) as crdate, category, 1 as isnew FROM `tx_stuff_domain_model_stuff` group by category) as j2
    on j.crdate=j2.crdate and j.category=j2.category

(If I have to write my own Query I will have to check deleted, hidden, starttime, endtime to check if the record is active right now)

My question now is, what is the cleanest/best way to add that to my extension?

2

There are 2 answers

1
Jost On

My solution would be to implement the method findAll in the job repository. It would first use the method findAll from the parent class to get all jobs. The usual constraints will already be applied. Then it would walk the resulting jobs and mark the newest one.

It should also be possible to create a custom query to reach you goal, but that would probably be a bite more work.

0
biesior On

Actually you don't need to use additional query at all... while it's always first record which is new, you can just use an iterator of for ViewHelper:

<f:for each="{stuffs}" as="stuff" iteration="iterator">

   <f:if condition="{iterator.isFirst}">THIS ONE IS BRAND NEW STUFF! </f:if>

   {stuff.title} <br />
   {stuff.category}
</f:for>