Frage

So I have a model site(#id, name, start_date, end_date) which might be in pause(#id, site_id, start_date, end_date) and I want to keep track of pauses.

I started by adding an expander button in the site CRUD to be able to add/remove pauses. Works like a charm.

But now I would like to know directly in the site list the state of each. So I started by adding a new expression field and just test if the site is in progress or not:

$this->addExpression('state')->set('if(site.end_date IS NULL,"In Progress", if(now() < site.end_date, "In Progress", "Ended"))');

Works perfectly ! But now I can't think of the method to determine if the site is in pause or not and in fact I cannot reproduice the result above using addExpression()->set(function($m, $q) {});

Edit:

So the example given by DarkSide works like a charm, still I need now to mix the two result into an only field state:

$this->addExpression('state')->set(function($model, $select)
 {
   // Is it ended ?
   $ended = $select->expr('if(site.date_ended IS NULL,"In Progress", if(now() < site.date_ended, "In Progress", "Ended"))');

   // Is it in pause ?
   $paused = $model->refSQL('Site_Pause')
                   ->count()
                   ->where('date_started', '<', $select->expr('now()'))
                   ->where('date_ended', '>', $select->expr('now()'));

  if ($paused > 0)
    return 'paused';
  return $ended;
 });

As $model->refSQL() returns a DSQL Object I cannot use of if statements. Should I try to make a all in once SQL query or try like above to use if statements by getting the results from Dsql object ?

War es hilfreich?

Lösung 2

Ok got it ! I post here the solution if needed by anyone:

$this->addExpression('etat')->set(function($model, $select) { return ( $select->expr( 'IF ( site.date_ended IS NULL OR NOW() < site.date_ended, IF ( [f1] > 0, "Paused", "In Progress" ), "Ended" )' )->setCustom('f1', $model->refSQL('Site_Pause') ->count() ->where('date_started', '<', $select->expr('NOW()')) ->where('date_ended', '>', $select->expr('NOW()')) ) ); });

Andere Tipps

In Model_Site:

Bad example:

I guess You can add everything in subselect in set() method as expression, but that's nor nice nor advisable.

$this->addExpression('paused')
    ->set("if( (SELECT 1 FROM pause WHERE pause.start_date<now() AND (pause.end_date IS NULL OR pause.end_date>now())), 'yes', 'no') ");

Better example:

$this->addExpression('paused')->set(function($m, $q){
    return $m->refSQL('Pause') // Model_Pause
        ->where('start_date', '<', $q->expr('now()'))
        ->where('end_date', '>', $q->expr('now()')) // here you'll also need to use q->orExpr() to add case when end_date is null (see expression above in bad example)
        ->count()
        ;
);

Something like this you can see in my Scheduler add-on Models (last_status, last_run, next_status, next_run in Model_Task). Here is link: https://github.com/DarkSide666/ds-addons/tree/master/Scheduler/lib/Model/Scheduler

Please keep in mind, that all examples above are completely untested and are here just to give you idea.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top