开发者

How to apply query to model if field not filtered

I have a range of modules running on generator.yml. In some of those I would like to hide records by default of Status: CLOSED (being the last of a开发者_StackOverflow中文版 range of Statuses). OF course if the user filters for CLOSED i want to show these records.

I thought it would make sense to apply andWhere('status_id=?',Status::CLOSED) in a specific table_method , but how do I access the filters of the module from within the model?

Is there a better way to do this?


I think you can create a new field e.g. 'showClosedStatus' (you ll need to add the widget and validator in the form) then add getFilterDefaults() in the generatorconfiguration with

public function getFilterDefaults() {
return array('showClosedStatus' => 'false');
}

then add the function addShowClosedStatusColumnQuery(Doctrine_Query $q, $field, $value){
if($value == false){
$q->andWhere('status!=closed')
}


I'm not 100% sure what you want to do? Is it to default the state of one of the filters?

If so, that is fairly easily done. apps//modules//lib/GeneratorConfiguration.php add a method to the class called "getFilterDefaults":

class modulenameGeneratorConfiguration extends BaseModulenameGeneratorConfiguration
{
  public function getFilterDefaults() {
    return array('status' => 'something');
  }

}

Only difference for you is that it sounds like you can filter on an array of values for the field in question. Never had cause to try this, but I'd imagine returning an array of values in place of something would be the way to do that.


Alternate answer, based on the comment from OP.

If you want to adjust how the query is constructed for a given field, find the relevant filter form in lib/filter/doctrine and add a method like this to it:

public function addStatusColumnQuery(Doctrine_Query $q, $field, $value) {
  // do whatever you want here - ie: check $value, and add the query conditions you need to $q.

  return $q;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜