Author: francois
Date: 2010-05-17 16:11:00 +0200 (Mon, 17 May 2010)
New Revision: 29493

Modified:
   plugins/sfPropel15Plugin/trunk/README
   plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php
   
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php
Log:
[sfPropel15Plugin] Added the ability to set query_methods on routes

Modified: plugins/sfPropel15Plugin/trunk/README
===================================================================
--- plugins/sfPropel15Plugin/trunk/README       2010-05-17 13:26:14 UTC (rev 
29492)
+++ plugins/sfPropel15Plugin/trunk/README       2010-05-17 14:11:00 UTC (rev 
29493)
@@ -119,18 +119,39 @@
 - **Plain text widget and validator**: This new widget allows a field to be 
displayed in a form, without letting the use change it.
 - **Easy Relation Embed**: Editing related objects together with the main 
objects (e.g., editing `Comments` in a `Post` form) is a piece of cake. The new 
`sfFormPropel::embedRelation()` method does all the work to fetch related 
objects, build the forms for each of them, and embed the related object forms 
into the main form. Embdeded relation forms allow to **edit**, **add**, and 
**delete** a related objects with no additional code.
 
-    [php]
-    class ArticleForm extends BaseArticleForm
-    {
-      public function configure()
-      {
-        $this->embedRelation('Book');
-      }
-    }
+        [php]
+        class ArticleForm extends BaseArticleForm
+        {
+          public function configure()
+          {
+            $this->embedRelation('Book');
+          }
+        }
 
 The Propel widgets, validators, and form classes are fully documented in the 
[`doc/form.txt`](http://trac.symfony-project.org/browser/plugins/sfPropel15Plugin/trunk/doc/form.txt)
 file in this plugin source code.
 
 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.
\ No newline at end of file
+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()`.
+
+    author:
+      class: sfPropel15RouteCollection
+      options:
+        model:                author
+        module:               author
+        prefix_path:          /author
+        column:               id
+        query_methods:        
+          object: [filterByIsPublished]
+          list:   [filterByIsPublished, orderByLastName]
+        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]
+    public function executeShow(sfWebRequest $request)
+    {
+      // using sfPropel15Route with 'Author' as model
+      $this->author = $this->getRoute()->getAuthor();
+    }

Modified: plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php        
2010-05-17 13:26:14 UTC (rev 29492)
+++ plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php        
2010-05-17 14:11:00 UTC (rev 29493)
@@ -106,9 +106,8 @@
     }
 
     // check the related object
-    $this->object = $this->getQuery()
-      ->filterByArray($this->getModelParameters($this->parameters))
-      ->findOne();
+    $this->object = $this->getQuery()->findOne();
+    
     if (!$this->object && (!isset($this->options['allow_empty']) || 
!$this->options['allow_empty']))
     {
       throw new sfError404Exception(sprintf('Unable to find the %s object with 
the following parameters "%s").', $this->options['model'], str_replace("\n", 
'', var_export($this->filterParameters($this->parameters), true))));
@@ -141,9 +140,7 @@
       return $this->objects;
     }
 
-               $this->object = $this->getQuery()
-                 ->filterByArray($this->getModelParameters($this->parameters))
-                 ->find();
+    $this->objects = $this->getQuery()->find();
 
     if (!count($this->objects) && isset($this->options['allow_empty']) && 
!$this->options['allow_empty'])
     {
@@ -152,10 +149,18 @@
 
     return $this->objects;
   }
-       
+  
   protected function getQuery()
   {
-       return PropelQuery::from($this->options['model']);
+    $query = PropelQuery::from($this->options['model']);
+    if (isset($this->options['query_methods']))
+    {
+      foreach ($this->options['query_methods'] as $methodName) {
+        $query->$methodName();
+      }
+    }
+    $query->filterByArray($this->getModelParameters($this->parameters));
+    return $query;
   }
   
   protected function getModelParameters($parameters)
@@ -165,7 +170,7 @@
       return $parameters;
     }
     $parameters = $this->filterParameters($parameters);
-               $peerName = constant($this->options['model'] . '::PEER');
+    $peerName = constant($this->options['model'] . '::PEER');
     $params = array();
     foreach ($this->getRealVariables() as $variable)
     {

Modified: 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php
===================================================================
--- 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php  
    2010-05-17 13:26:14 UTC (rev 29492)
+++ 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php  
    2010-05-17 14:11:00 UTC (rev 29493)
@@ -20,4 +20,154 @@
 {
   protected
     $routeClass = 'sfPropel15Route';
+
+  /**
+   * Constructor.
+   *
+   * @param array $options An array of options
+   */
+  public function __construct(array $options)
+  {
+    $this->options = array_merge(array(
+      'query_methods'        => array(),
+    ), $this->options);
+    
+    $this->options['query_methods'] = array_merge(array('list' => null, 
'object' => null), $this->options['query_methods']);
+
+    parent::__construct($options);
+  }
+
+  protected function generateRoutes()
+  {
+    // collection actions
+    if (isset($this->options['collection_actions']))
+    {
+      foreach ($this->options['collection_actions'] as $action => $methods)
+      {
+        $this->routes[$this->getRoute($action)] = 
$this->getRouteForCollection($action, $methods);
+      }
+    }
+
+    // "standard" actions
+    $actions = false === $this->options['actions'] ? 
$this->getDefaultActions() : $this->options['actions'];
+    foreach ($actions as $action)
+    {
+      $method = 'getRouteFor'.ucfirst($action);
+      if (!method_exists($this, $method))
+      {
+        throw new InvalidArgumentException(sprintf('Unable to generate a route 
for the "%s" action.', $action));
+      }
+
+      $this->routes[$this->getRoute($action)] = $this->$method();
+    }
+
+    // object actions
+    if (isset($this->options['object_actions']))
+    {
+      foreach ($this->options['object_actions'] as $action => $methods)
+      {
+        $this->routes[$this->getRoute($action)] = 
$this->getRouteForObject($action, $methods);
+      }
+    }
+
+    if ($this->options['with_wildcard_routes'])
+    {
+      // wildcard object actions
+      $this->routes[$this->getRoute('object')] = 
$this->getWildcardRouteForObject();
+
+      // wildcard collection actions
+      $this->routes[$this->getRoute('collection')] = 
$this->getWildcardRouteForCollection();
+    }
+  }
+
+  protected function getRouteForCollection($action, $methods)
+  {
+    return new $this->routeClass(
+      sprintf('%s/%s.:sf_format', $this->options['prefix_path'], $action),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$action, 'sf_format' => 'html'), $this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 
$methods)),
+      array('model' => $this->options['model'], 'type' => 'list', 
'query_methods' => $this->options['query_methods']['list'])
+    );
+  }
+
+  protected function getRouteForObject($action, $methods)
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], 
$this->options['column'], $action),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$action, 'sf_format' => 'html'), $this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 
$methods)),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+  
+  protected function getWildcardRouteForCollection()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:action/action.:sf_format', $this->options['prefix_path']),
+      array_merge(array('module' => $this->options['module'], 'sf_format' => 
'html'), $this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 
'post')),
+      array('model' => $this->options['model'], 'type' => 'list', 
'query_methods' => $this->options['query_methods']['list'])
+    );
+  }
+
+  protected function getWildcardRouteForObject()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s/:action.:sf_format', $this->options['prefix_path'], 
$this->options['column']),
+      array_merge(array('module' => $this->options['module'], 'sf_format' => 
'html'), $this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 'get')),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+
+  protected function getRouteForList()
+  {
+    return new $this->routeClass(
+      sprintf('%s.:sf_format', $this->options['prefix_path']),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$this->getActionMethod('list'), 'sf_format' => 'html'), 
$this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 'get')),
+      array('model' => $this->options['model'], 'type' => 'list', 
'query_methods' => $this->options['query_methods']['list'])
+    );
+  }
+
+  protected function getRouteForShow()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], 
$this->options['column']),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$this->getActionMethod('show'), 'sf_format' => 'html'), 
$this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 'get')),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+
+  protected function getRouteForEdit()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], 
$this->options['column'], $this->options['segment_names']['edit']),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$this->getActionMethod('edit'), 'sf_format' => 'html'), 
$this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 'get')),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+
+  protected function getRouteForUpdate()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], 
$this->options['column']),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$this->getActionMethod('update'), 'sf_format' => 'html'), 
$this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 'put')),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+
+  protected function getRouteForDelete()
+  {
+    return new $this->routeClass(
+      sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], 
$this->options['column']),
+      array_merge(array('module' => $this->options['module'], 'action' => 
$this->getActionMethod('delete'), 'sf_format' => 'html'), 
$this->options['default_params']),
+      array_merge($this->options['requirements'], array('sf_method' => 
'delete')),
+      array('model' => $this->options['model'], 'type' => 'object', 
'query_methods' => $this->options['query_methods']['object'])
+    );
+  }
+
 }

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to