0

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?

nbar
  • 6,028
  • 2
  • 24
  • 65

2 Answers2

0

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.

Jost
  • 5,948
  • 8
  • 42
  • 72
  • I did it with a custom query right now. I not find out how I add the self created field (isnew). Maybe I have to add it to the Model with getter and setter? - At all it does not matter cause I need this field only for the initial sorting, so I dont need the value in the frontend as I can order the SQL Statement with the isnew field. – nbar Dec 17 '13 at 16:04
0

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>
biesior
  • 55,576
  • 10
  • 125
  • 182