diff --git a/README b/README index 0dc7c21..87de3af 100644 --- a/README +++ b/README @@ -201,6 +201,18 @@ Here is the default content of the `generator.yml` file: # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters # created_at: { date_format: 'd-m-Y', multiple: true } # for instance + # get_list: # overwrite "get" configuration for list + # embed_relations: [] # list here relations to embed in the response + # embedded_relations_hide: + # Category: [id] # you can hide fields inside a certain embedded relation + # hide: [id] # list here the fields you don't want to expose + # object_additional_fields: [] # list here additionnal calculated fields + # get_show: # overwrite "get" configuration for show + # embed_relations: [] # list here relations to embed in the response + # embedded_relations_hide: + # Category: [id] # you can hide fields inside a certain embedded relation + # hide: [id] # list here the fields you don't want to expose + # object_additional_fields: [] # list here additionnal calculated fields The different possible parameters, commented in the previous sample, are detailed in the following chapters. @@ -504,6 +516,16 @@ accepted. For example: filters: created_at: { date_format: 'd-m-Y' } +### get_list and get_show + +To be able to configure different fields to be part of output for list and show action, the `get_list` and `get_list` option allow to overwrite specific options of `get`: + + * embed_relations + * embedded_relations_hide + * hide + * object_additional_fields + + ### 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..87de3af 100644 --- a/README.markdown +++ b/README.markdown @@ -201,6 +201,18 @@ Here is the default content of the `generator.yml` file: # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters # created_at: { date_format: 'd-m-Y', multiple: true } # for instance + # get_list: # overwrite "get" configuration for list + # embed_relations: [] # list here relations to embed in the response + # embedded_relations_hide: + # Category: [id] # you can hide fields inside a certain embedded relation + # hide: [id] # list here the fields you don't want to expose + # object_additional_fields: [] # list here additionnal calculated fields + # get_show: # overwrite "get" configuration for show + # embed_relations: [] # list here relations to embed in the response + # embedded_relations_hide: + # Category: [id] # you can hide fields inside a certain embedded relation + # hide: [id] # list here the fields you don't want to expose + # object_additional_fields: [] # list here additionnal calculated fields The different possible parameters, commented in the previous sample, are detailed in the following chapters. @@ -504,6 +516,16 @@ accepted. For example: filters: created_at: { date_format: 'd-m-Y' } +### get_list and get_show + +To be able to configure different fields to be part of output for list and show action, the `get_list` and `get_list` option allow to overwrite specific options of `get`: + + * embed_relations + * embedded_relations_hide + * hide + * object_additional_fields + + ### Other configuration variables Some other configuration variables are not present in the default configuration file: diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/configuration.php b/data/generator/sfDoctrineRestGenerator/default/parts/configuration.php old mode 100644 new mode 100755 index 514ceee..59c55ca --- a/data/generator/sfDoctrineRestGenerator/default/parts/configuration.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/configuration.php @@ -24,8 +24,20 @@ public function getDefaultFormat() public function getDisplay() { - return asPhp(isset($this->config['get']['display']) ? $this->config['get']['display'] : array()) ?>; -config['get']['display']) ?> + return asPhp(isset($this->config['get']['display']) ? $this->config['get']['display'] : array()) ?>; + config['get']['display']) ?> + } + + public function getListEmbedRelations() + { + return asPhp(isset($this->config['get_list']['embed_relations']) ? $this->config['get_list']['embed_relations'] : (isset($this->config['get']['embed_relations']) ? $this->config['get']['embed_relations'] : array())) ?>; + config['get_list']['embed_relations']) ?> + } + + public function getShowEmbedRelations() + { + return asPhp(isset($this->config['get_show']['embed_relations']) ? $this->config['get_show']['embed_relations'] : (isset($this->config['get']['embed_relations']) ? $this->config['get']['embed_relations'] : array())) ?>; + config['get_show']['embed_relations']) ?> } public function getEmbedRelations() @@ -34,6 +46,32 @@ public function getEmbedRelations() config['get']['embed_relations']) ?> } + public function getListEmbeddedRelationsHide() + { + $embedded_relations_hide = asPhp(isset($this->config['get_list']['embedded_relations_hide']) ? $this->config['get_list']['embedded_relations_hide'] : (isset($this->config['get']['embedded_relations_hide']) ? $this->config['get']['embedded_relations_hide'] : array())) ?>; + + foreach ($embedded_relations_hide as $relation_name => $hidden_fields) + { + $embedded_relations_hide[$relation_name] = array_flip($hidden_fields); + } + + return $embedded_relations_hide; + config['get_list']['embedded_relations_hide']) ?> + } + + public function getShowEmbeddedRelationsHide() + { + $embedded_relations_hide = asPhp(isset($this->config['get_show']['embedded_relations_hide']) ? $this->config['get_show']['embedded_relations_hide'] : (isset($this->config['get']['embedded_relations_hide']) ? $this->config['get']['embedded_relations_hide'] : array())) ?>; + + foreach ($embedded_relations_hide as $relation_name => $hidden_fields) + { + $embedded_relations_hide[$relation_name] = array_flip($hidden_fields); + } + + return $embedded_relations_hide; + config['get_show']['embedded_relations_hide']) ?> + } + public function getEmbeddedRelationsHide() { $embedded_relations_hide = asPhp(isset($this->config['get']['embedded_relations_hide']) ? $this->config['get']['embedded_relations_hide'] : array()) ?>; @@ -44,7 +82,7 @@ public function getEmbeddedRelationsHide() } return $embedded_relations_hide; -config['get']['embedded_relations_hide']) ?> + config['get']['embedded_relations_hide']) ?> } public function getFormatsEnabled() @@ -67,10 +105,22 @@ public function getGlobalAdditionalFields() config['get']['global_additional_fields']) ?> } + public function getListHide() + { + return asPhp(isset($this->config['get_list']['hide']) ? $this->config['get_list']['hide'] : (isset($this->config['get']['hide']) ? $this->config['get']['hide'] : array())) ?>; + config['get_list']['hide']) ?> + } + + public function getShowHide() + { + return asPhp(isset($this->config['get_show']['hide']) ? $this->config['get_show']['hide'] : (isset($this->config['get']['hide']) ? $this->config['get']['hide'] : array())) ?>; + config['get_show']['hide']) ?> + } + public function getHide() { return asPhp(isset($this->config['get']['hide']) ? $this->config['get']['hide'] : array()) ?>; -config['get']['hide']) ?> + config['get']['hide']) ?> } public function getMaxItems() @@ -79,16 +129,28 @@ public function getMaxItems() config['get']['max_items']) ?> } + public function getListObjectAdditionalFields() + { + return asPhp(isset($this->config['get_list']['object_additional_fields']) ? $this->config['get_list']['object_additional_fields'] : (isset($this->config['get']['object_additional_fields']) ? $this->config['get']['object_additional_fields'] : array())) ?>; + config['get_list']['object_additional_fields']) ?> + } + + public function getShowObjectAdditionalFields() + { + return asPhp(isset($this->config['get_show']['object_additional_fields']) ? $this->config['get_show']['object_additional_fields'] : (isset($this->config['get']['object_additional_fields']) ? $this->config['get']['object_additional_fields'] : array())) ?>; + config['get_show']['object_additional_fields']) ?> + } + public function getObjectAdditionalFields() { return asPhp(isset($this->config['get']['object_additional_fields']) ? $this->config['get']['object_additional_fields'] : array()) ?>; -config['get']['object_additional_fields']) ?> + config['get']['object_additional_fields']) ?> } public function getSeparator() { return asPhp(isset($this->config['default']['separator']) ? $this->config['default']['separator'] : ',') ?>; -config['default']['separator']) ?> + config['default']['separator']) ?> } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php old mode 100644 new mode 100755 index 4997beb..1c50c49 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -57,28 +57,29 @@ public function executeIndex(sfWebRequest $request) $this->forward404(); } -configuration->getValue('get.embed_relations'); ?> +getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_LIST, 'embed_relations'); ?> -isManyToManyRelation($embed_relation)): ?> - $this->embedManyToMany($params); - -configuration->getValue('get.object_additional_fields'); ?> + isManyToManyRelation($embed_relation)): ?> + $this->embedManyToMany($params); + + +getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_LIST, 'object_additional_fields'); ?> 0): ?> - foreach ($this->objects as $key => $object) { - + $this->embedAdditional($key, $params); - + } -configuration->getValue('get.global_additional_fields'); ?> + +configuration->getValue('get.global_additional_fields'); ?> $this->embedGlobalAdditional($params); // configure the fields of the returned objects and eventually hide some - $this->setFieldVisibility(); + $this->setFieldVisibilityList(); $this->configureFields(); $serializer = $this->getSerializer(); diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/query.php b/data/generator/sfDoctrineRestGenerator/default/parts/query.php old mode 100644 new mode 100755 index 4e3b68c..2b37faf --- a/data/generator/sfDoctrineRestGenerator/default/parts/query.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/query.php @@ -4,31 +4,34 @@ * * @param array $params an array of criterions for the selection */ - public function query($params) + public function query($params) { $q = Doctrine_Query::create() -configuration->getValue('get.display'); -$embed_relations = $this->configuration->getValue('get.embed_relations'); +configuration->getValue('get.display'); ?> + +getConfigurationGetValue($mode, 'embed_relations'); ?> + + 0): ?> - - ->select('') + ->select(''); - ->from($this->model.' '.$this->model) - -isManyToManyRelation($embed_relation)): ?> + ->from($this->model.' '.$this->model) - ->leftJoin($this->model.'. '); + + isManyToManyRelation($embed_relation)): ?> + ->leftJoin($this->model.'. ') + +; - $this->queryPagination($q, $params); + $this->queryPagination($q, $params); configuration->getValue('get.sort_custom'); ?> configuration->getValue('get.sort_default'); ?> diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryAdditionnal.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryAdditionnal.php old mode 100644 new mode 100755 index 28f1f7d..0aa174a --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryAdditionnal.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/queryAdditionnal.php @@ -1,5 +1,9 @@ configuration->getValue('get.embed_relations'); +$embed_relations = array_unique(array_merge( + $this->getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_LIST, 'embed_relations', array()), + $this->getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_SHOW, 'embed_relations', array()), + $this->configuration->getValue('get.embed_relations', array()) +)); $pk = current($this->getPrimaryKeys()); ?> @@ -60,7 +64,13 @@ public function embedManyToMany() } } -configuration->getValue('get.object_additional_fields'); ?> +getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_LIST, 'object_additional_fields', array()), + $this->getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_SHOW, 'object_additional_fields', array()), + $this->configuration->getValue('get.object_additional_fields', array()) + )); +?> /** diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php old mode 100644 new mode 100755 index 2b72f16..2b75220 --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php @@ -12,6 +12,6 @@ public function queryExecute($params) 'sfDoctrineRestGenerator.filter_results', array() ), - $this->query($params)->execute(array(), Doctrine_Core::HYDRATE_ARRAY) + $this->queryList($params)->execute(array(), Doctrine_Core::HYDRATE_ARRAY) )->getReturnValue(); } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php old mode 100644 new mode 100755 index db7c0fe..3826084 --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php @@ -12,6 +12,6 @@ public function queryFetchOne($params) 'sfDoctrineRestGenerator.filter_result', array() ), - $this->query($params)->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY) + $this->queryShow($params)->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY) )->getReturnValue()); } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryPagination.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryPagination.php index 4abf485..43c8e67 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryPagination.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/queryPagination.php @@ -40,12 +40,12 @@ protected function queryPagination(Doctrine_Query $query, array &$params) $limit = $page_size; - $q->offset(($params['page'] - 1) * $page_size); + $query->offset(($params['page'] - 1) * $page_size); unset($params['page']); 0 || $pagination_enabled): ?> - $q->limit($limit); + $query->limit($limit); - return $q; + return $query; } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/setFieldVisibility.php b/data/generator/sfDoctrineRestGenerator/default/parts/setFieldVisibility.php old mode 100644 new mode 100755 index f2aa81d..3e520a9 --- a/data/generator/sfDoctrineRestGenerator/default/parts/setFieldVisibility.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/setFieldVisibility.php @@ -4,14 +4,15 @@ * * @return void */ - protected function setFieldVisibility() + protected function setFieldVisibility() { configuration->getValue('get.display'); -$hide = $this->configuration->getValue('get.hide'); -$embed_relations = $this->configuration->getValue('get.embed_relations'); -$object_additional_fields = $this->configuration->getValue('get.object_additional_fields'); -?> 0): ?> +$hide = $this->getConfigurationGetValue($mode, 'hide'); +$embed_relations = $this->getConfigurationGetValue($mode, 'embed_relations'); +$object_additional_fields = $this->getConfigurationGetValue($mode, 'object_additional_fields'); +?> + 0): ?> 0): ?> $accepted_keys = ; @@ -37,7 +38,7 @@ protected function setFieldVisibility() } } -configuration->getValue('get.embedded_relations_hide'); ?> +getConfigurationGetValue($mode, 'embedded_relations_hide'); ?> 0): ?> $embedded_relations_hide = ; diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php old mode 100644 new mode 100755 index 935934f..9d613ec --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -49,14 +49,14 @@ public function executeShow(sfWebRequest $request) $this->queryFetchOne($params); $this->forward404Unless(is_array($this->objects[0])); -configuration->getValue('get.object_additional_fields') as $field): ?> +getConfigurationGetValue(sfDoctrineRestGenerator::GET_MODE_SHOW, 'object_additional_fields') as $field): ?> $this->embedAdditional(0, $params); configuration->getValue('get.global_additional_fields') as $field): ?> $this->embedGlobalAdditional($params); - $this->setFieldVisibility(); + $this->setFieldVisibilityShow(); $this->configureFields(); $serializer = $this->getSerializer(); diff --git a/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml b/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml old mode 100644 new mode 100755 index 9a437f1..efd8d31 --- a/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml +++ b/data/generator/sfDoctrineRestGenerator/default/skeleton/config/generator.yml @@ -28,3 +28,15 @@ generator: # sort_default: [] # set to [column, asc|desc] in order to sort on a column # filters: # list here the filters # created_at: { date_format: 'd-m-Y', multiple: true } # for instance +# get_list: # overwrite "get" configuration for list +# embed_relations: [] # list here relations to embed in the response +# embedded_relations_hide: +# Category: [id] # you can hide fields inside a certain embedded relation +# hide: [id] # list here the fields you don't want to expose +# object_additional_fields: [] # list here additionnal calculated fields +# get_show: # overwrite "get" configuration for show +# embed_relations: [] # list here relations to embed in the response +# embedded_relations_hide: +# Category: [id] # you can hide fields inside a certain embedded relation +# hide: [id] # list here the fields you don't want to expose +# object_additional_fields: [] # list here additionnal calculated fields diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php old mode 100644 new mode 100755 index ba71a7a..21a5cf2 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -50,7 +50,13 @@ class getGeneratedModuleName() ?>Actions extends + + + + + + @@ -59,6 +65,11 @@ class getGeneratedModuleName() ?>Actions extends + + + + + diff --git a/lib/generator/sfDoctrineRestGenerator.class.php b/lib/generator/sfDoctrineRestGenerator.class.php old mode 100644 new mode 100755 index f6c6fc2..a7b0578 --- a/lib/generator/sfDoctrineRestGenerator.class.php +++ b/lib/generator/sfDoctrineRestGenerator.class.php @@ -18,6 +18,10 @@ */ class sfDoctrineRestGenerator extends sfGenerator { + const GET_MODE_LIST = 'list'; + + const GET_MODE_SHOW = 'show'; + protected $configuration = null, $primaryKey = array(), @@ -946,4 +950,16 @@ public function getValidatorOptionsForColumn($column) return count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''; } + + protected function getConfigurationGetValue($mode, $parameter, $default = null, $escaped = false) + { + if (empty($mode)) { + return $this->configuration->getValue('get.'.$parameter, $default, $escaped); + } + + return $this->configuration->getValue( + 'get_'.$mode.'.'.$parameter, + $this->configuration->getValue('get.'.$parameter, $default, $escaped) + ); + } } diff --git a/lib/generator/sfDoctrineRestGeneratorConfiguration.class.php b/lib/generator/sfDoctrineRestGeneratorConfiguration.class.php old mode 100644 new mode 100755 index 421cc02..fd05ae6 --- a/lib/generator/sfDoctrineRestGeneratorConfiguration.class.php +++ b/lib/generator/sfDoctrineRestGeneratorConfiguration.class.php @@ -39,6 +39,18 @@ protected function compile() 'pagination_page_size' => $this->getPaginationPageSize(), 'sort_custom' => $this->getSortCustom(), 'sort_default' => $this->getSortDefault() + ), + 'get_list' => array( + 'embed_relations' => $this->getListEmbedRelations(), + 'embedded_relations_hide' => $this->getListEmbeddedRelationsHide(), + 'hide' => $this->getListHide(), + 'object_additional_fields' => $this->getListObjectAdditionalFields(), + ), + 'get_show' => array( + 'embed_relations' => $this->getShowEmbedRelations(), + 'embedded_relations_hide' => $this->getShowEmbeddedRelationsHide(), + 'hide' => $this->getShowHide(), + 'object_additional_fields' => $this->getShowObjectAdditionalFields(), ) ); }