Permalink
Browse files

query methods:

 - updated documentation
 - bugfixes
  • Loading branch information...
1 parent c453dfe commit 09f08af2ddc9518fddd73ab43b84ef4b61239eb9 rozwell committed May 2, 2011
View
@@ -139,6 +139,7 @@ Routing Modifications
The plugin offer two new routing classes, `sfPropel15Route` and `sfPropel15RouteCollection`. These classes are used by default in the models build with the propel admin generator. They behave just like the previous `sfPropelRoute` class - except they don't use the `methods` option anymore. Instead, use the `query_methods` option to execute a list of arbitrary query methods when calling `getObject()` and `getObjects()`.
+ [yaml]
author:
class: sfPropel15RouteCollection
options:
@@ -151,6 +152,24 @@ The plugin offer two new routing classes, `sfPropel15Route` and `sfPropel15Route
list: [filterByIsPublished, orderByLastName]
with_wildcard_routes: true
+Array of additional parameters are also possible for `query_methods`:
+
+ [yaml]
+ author:
+ class: sfPropel15RouteCollection
+ options:
+ model: author
+ module: author
+ prefix_path: /author
+ column: id
+ query_methods:
+ object:
+ filterByIsPublished: [false]
+ list:
+ filterByIsPublished: []
+ orderBy: [LastName]
+ with_wildcard_routes: true
+
`sfPropel15Route` also makes your code a little easier to read in the action. Instead of calling `getObject()`, you can actually call a getter using the class name of the object's route:
[php]
@@ -34,11 +34,18 @@ protected function buildQuery()
foreach ($this->configuration->getWiths() as $with) {
$query->joinWith($with);
}
-
- foreach ($this->configuration->getQueryMethods() as $method) {
- $query->$method();
+
+ foreach ($this->configuration->getQueryMethods() as $methodName => $methodParams) {
+ if(is_array($methodParams))
+ {
+ call_user_func_array(array($query, $methodName), $methodParams);
+ }
+ else
+ {
+ $query->$methodParams();
+ }
}
-
+
$this->processSort($query);
$event = $this->dispatcher->filter(new sfEvent($this, 'admin.build_criteria'), $query);
View
@@ -74,7 +74,7 @@ You must implement each `query_method` in the main object's query class. In this
return $this->filterByPublishedAt(array('min' => time()));
}
}
-
+
You can use this feature to add calculated columns to the list without additional queries:
[yaml]
@@ -100,6 +100,15 @@ Now you can add a partial column and use the virtual `NbReviews` column in the l
[php]
<?php echo $book->getVirtualColumn('NbReviews') ?>
+Sometimes you may want to add additional parameter(s) to `query_method`. You can do that by adding array to specific `query_method`. This example does the same as previous but shows how to pass parameters:
+
+ [yaml]
+ list:
+ display: [title]
+ query_methods:
+ leftJoin: ['Book.Review']
+ withColumn: ['COUNT(Review.Id)', 'NbReviews']
+
Sorting On A Virtual Column
---------------------------
@@ -110,7 +119,7 @@ The new theme provides an easy way to make virtual columns and foreign key colum
list:
display: [title, Author]
query_methods: [joinWithAuthor]
- fields:
+ fields:
- Author: { is_sortable: true }
Then the generator will try to execute `BookQuery::orderByAuthor()` whenever the user clicks on the `Author` header to sort on this column. The method must be implemented as follows:
@@ -134,10 +143,10 @@ You can override the default sorting method name for a field by setting the `sor
list:
display: [title, Author]
query_methods: [joinWithAuthor]
- fields:
+ fields:
- Author: { is_sortable: true, sort_method: orderByAuthorLastName }
-
-The generator will execute `BookQuery::orderByAuthorLastName()` instead of `BookQuery::orderByAuthor()` in that case.
+
+The generator will execute `BookQuery::orderByAuthorLastName()` instead of `BookQuery::orderByAuthor()` in that case.
Filtering The List With GET Parameters
--------------------------------------
@@ -175,15 +184,15 @@ The `admin15` theme provides a shortcut for this situation. Just define the `lin
display: [title, =Author]
fields:
Author: { link_module: author }
-
+
You no longer need a partial for such simple cases. This should unclutter the `templates/` directory of your admin generator modules.
Easy Custom Filter
------------------
Adding custom filters becomes very easy once you can take advantage of the generated Propel query classes. For example, in a list of `Books`, the default filters offer one text input for the book title, and a second one for the book ISBN. In order to replace them with a single text input, here is what you need to do:
- [php]
+ [php]
// in lib/filter/BookFormFilter.php
class BookFormFilter extends BaseBookFormFilter
{
@@ -193,7 +202,7 @@ Adding custom filters becomes very easy once you can take advantage of the gener
$this->validatorSchema['full_text'] = new sfValidatorPass(array('required' => false));
}
}
-
+
// in lib/model/Bookquery.php
class BookQuery extends BaseBookQuery
{
@@ -234,7 +243,7 @@ For instance, to replace an input filter on a text column by a choice list, try
[php]
filter:
fields:
- sex:
+ sex:
widgetClass: sfWidgetFormChoice
widgetOptions: { choices: { '': '' , male: Male, female: Female } }
@@ -243,7 +252,7 @@ To remove the "is empty" checkbox for a given filter, just set the relevant widg
[php]
filter:
fields:
- title:
+ title:
widgetOptions: { with_empty: false }
To replace a text input by a textarea in the edit form, change the widget class:
View
@@ -57,6 +57,20 @@ You can set the widget to execute additional query methods on the related Model
}
}
+Query methods can also accept array of parameters, for example to sort branch in reverse order:
+
+ [php]
+ class ContentForm extends BaseContentForm
+ {
+ public function configure()
+ {
+ $this->widgetSchema['section'] = new sfWidgetFormPropelChoice(array(
+ 'model' => 'Section',
+ 'query_methods' => array('orderByBranch' => array(true))
+ 'add_empty' => true,
+ ));
+ }
+ }
You can also enable the `query_method` option on an existing widget. For instance, to display only the list of active authors, customize the form as follows:
@@ -159,11 +159,11 @@ protected function getQuery()
{
if(is_array($methodParams))
{
- call_user_func_array(array($criteria, $methodName), $methodParams);
+ call_user_func_array(array($query, $methodName), $methodParams);
}
else
{
- $criteria->$methodParams();
+ $query->$methodParams();
}
}
}

0 comments on commit 09f08af

Please sign in to comment.