diff --git a/README b/README index 0dc7c21..6ee770e 100644 --- a/README +++ b/README @@ -200,6 +200,8 @@ Here is the default content of the `generator.yml` file: # sort_custom: false # set to true to allow the client to pass a sort_by and a sort_order parameter # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters + # id: + # compare: { less: , greaterEqual: moreThan } # generate filters idLess and idMoreThan # created_at: { date_format: 'd-m-Y', multiple: true } # for instance The different possible parameters, commented in the previous sample, are @@ -504,6 +506,31 @@ accepted. For example: filters: created_at: { date_format: 'd-m-Y' } +To compare a certain field with a given value, you can add a `compare` +directive to add filters to a certain field. +These filters take the name `fieldNameOperator`, the following operators are +supported: + + * less + * lessEqual + * greater + * greaterEqual + +Optionally, the compare filters take an alias name, which overrides the default +operator names in the parameter. + +For example, to add compare filters to the `created_at` field: + + config: + get: + filters: + created_at: + compare: + less: before # generates the created_atBefore filter + lessEqual: before # generates the created_atLessEqual filter + greater: after # generates the created_atAfter filter + # greaterEqual will not be generated + ### Other configuration variables Some other configuration variables are not present in the default configuration file: diff --git a/README.markdown b/README.markdown index 0dc7c21..6ee770e 100644 --- a/README.markdown +++ b/README.markdown @@ -200,6 +200,8 @@ Here is the default content of the `generator.yml` file: # sort_custom: false # set to true to allow the client to pass a sort_by and a sort_order parameter # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters + # id: + # compare: { less: , greaterEqual: moreThan } # generate filters idLess and idMoreThan # created_at: { date_format: 'd-m-Y', multiple: true } # for instance The different possible parameters, commented in the previous sample, are @@ -504,6 +506,31 @@ accepted. For example: filters: created_at: { date_format: 'd-m-Y' } +To compare a certain field with a given value, you can add a `compare` +directive to add filters to a certain field. +These filters take the name `fieldNameOperator`, the following operators are +supported: + + * less + * lessEqual + * greater + * greaterEqual + +Optionally, the compare filters take an alias name, which overrides the default +operator names in the parameter. + +For example, to add compare filters to the `created_at` field: + + config: + get: + filters: + created_at: + compare: + less: before # generates the created_atBefore filter + lessEqual: before # generates the created_atLessEqual filter + greater: after # generates the created_atAfter filter + # greaterEqual will not be generated + ### Other configuration variables Some other configuration variables are not present in the default configuration file: diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php index 8763637..05a5754 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php @@ -33,6 +33,20 @@ public function getIndexValidators() $validators[''] = new sfValidatorPass(array('required' => false)); + + +configuration->getValue('get.filters'); ?> + + $filter): ?> + + $opAlias) : ?> + + $validators[''] = new sfValidatorPass(array( + 'required' => false, + )); + + + return $validators; diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/query.php b/data/generator/sfDoctrineRestGenerator/default/parts/query.php index 101fae1..19bd4e3 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/query.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/query.php @@ -130,6 +130,22 @@ public function query($params) unset($params['']); } + + '<', + 'lessEqual' => '<=', + 'greater' => '>', + 'greaterEqual' => '>=', +); ?> + $opAlias): ?> + + if (isset($params[''])) + { + $q->andWhere($this->model.'. ?', $params['']); + unset($params['']); + } + + foreach ($params as $name => $value) { diff --git a/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml b/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml index 9a437f1..f68b141 100644 --- a/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml +++ b/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml @@ -27,4 +27,6 @@ generator: # sort_custom: false # set to true to allow the client to pass a sort_by and a sort_order parameter # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters +# id: +# compare: { less: , greaterEqual: moreThan } # generate filters idLess and idMoreThan # created_at: { date_format: 'd-m-Y', multiple: true } # for instance