Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public function getIndexValidators()
<?php foreach ($additional_params as $param): ?>
$validators['<?php echo $param; ?>'] = new sfValidatorPass(array('required' => false));
<?php endforeach; ?>
<?php endif; ?>

<?php $filters = $this->configuration->getValue('get.filters'); ?>
<?php if ($filters): ?>
<?php foreach ($filters as $name => $filter): ?>
<?php if (isset($filter['compare'])): ?>
<?php foreach ($filter['compare'] as $operator => $opAlias) : ?>
<?php $opAlias = ucfirst($opAlias == null ? $operator : $opAlias); ?>
$validators['<?php echo $name . $opAlias ?>'] = new sfValidatorPass(array(
'required' => false,
));
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>

return $validators;
Expand Down
16 changes: 16 additions & 0 deletions data/generator/sfDoctrineRestGenerator/default/parts/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ public function query($params)
unset($params['<?php echo $name ?>']);
}
<?php endif; ?>
<?php if (isset($filters[$name]['compare'])): ?>
<?php $operators = array(
'less' => '<',
'lessEqual' => '<=',
'greater' => '>',
'greaterEqual' => '>=',
); ?>
<?php foreach ($filters[$name]['compare'] as $operation => $opAlias): ?>
<?php $opAlias = $opAlias == null ? $operation : $opAlias; ?>
if (isset($params['<?php echo $name . ucfirst($opAlias) ?>']))
{
$q->andWhere($this->model.'.<?php echo $name ?> <?php echo $operators[$operation] ?> ?', $params['<?php echo $name . ucfirst($opAlias) ?>']);
unset($params['<?php echo $name . ucfirst($opAlias) ?>']);
}
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
foreach ($params as $name => $value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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