Automating Object Ordering in Symfony

The next logical step to my last post Automating __toString in Symfony would be to automate the ordering of objects that are obvious – eg. – that have a name, title, (last_name, first_name), or precedence fields. By automatically adding a peer method that would order the objects, and would be referenced in the CRUD(both in selects in the edit forms, and in the lists) when needed, we provide yet another shortcut in our model preparation. I include context diffs below that accomplish this.

Index: plugins/sfPropelPlugin/data/generator/sfPropelAdmin/default/template/actions/actions.class.php
===================================================================
--- plugins/sfPropelPlugin/data/generator/sfPropelAdmin/default/template/actions/actions.class.php   (revision 3)
+++ plugins/sfPropelPlugin/data/generator/sfPropelAdmin/default/template/actions/actions.class.php   (working copy)
@@ -34,6 +34,8 @@
     $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/getSingularName() ?>')));
 getParameterValue('list.peer_method')): ?>
     $this->pager->setPeerMethod('getParameterValue('list.peer_method') ?>');
+getPeerClassName() . '::doSelectOrdered')): ?>
+    $this->pager->setPeerMethod('doSelectOrdered');
 
 getParameterValue('list.peer_count_method')): ?>
     $this->pager->setPeerCountMethod('getParameterValue('list.peer_count_method') ?>');

Index: helper/ObjectHelper.php
===================================================================
--- helper/ObjectHelper.php     (revision 3)
+++ helper/ObjectHelper.php     (working copy)
@@ -117,6 +117,16 @@

   $peer_method = _get_option($options, 'peer_method');

+  if(!$peer_method){
+      $ordered_select = 'doSelectOrdered';
+
+      $classPeer = constant($related_class.'::PEER');
+
+      if(is_callable(array($classPeer, $ordered_select))){
+          $peer_method = $ordered_select;
+      }
+  }
+
   $text_method = _get_option($options, 'text_method');

   $key_method = _get_option($options, 'key_method', 'getPrimaryKey');

Index: plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/PeerBuilder.php
===================================================================
--- plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/PeerBuilder.php (revision 3)
+++ plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/PeerBuilder.php (working copy)
@@ -63,6 +63,7 @@
                // consider refactoring the doSelect stuff
                // into a top-level method
                $this->addDoSelectOne($script);
+               $this->addDoSelectOrdered($script);
                $this->addDoSelect($script);
                $this->addDoSelectStmt($script);         // <-- there's PDO code in here

Index: plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ObjectBuilder.php
===================================================================
--- plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5PeerBuilder.php        (revision 3)
+++ plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5PeerBuilder.php        (working copy)
@@ -534,6 +534,54 @@
        }

        /**
+        * Adds the doSelectOrdered() method.
+        * @param      string &$script The script will be modified in this method.
+        */
+       protected function addDoSelectOrdered(&$script)
+       {
+           foreach ($this->getTable()->getColumns() as $col) {
+                $column_names[] = $col->getName();
+            }
+
+            if(in_array('name', $column_names)){
+                $orderByColumn = 'NAME';
+            }
+            elseif(in_array('last_name', $column_names)){
+                $orderByColumn = 'LAST_NAME';
+                if(in_array('first_name', $column_names)){
+                    $secondOrderByColumn = 'FIRST_NAME';
+                }
+            }
+            elseif(in_array('title', $column_names)){
+                $orderByColumn = 'TITLE';
+            }
+            elseif(in_array('precedence', $column_names)){
+                $orderByColumn = 'PRECEDENCE';
+            }
+            else{
+               return;
+            }
+
+           $script .= "
+       /**
+        * Method to select an ordered set of objects from the DB.
+        *
+        * @param      Criteria \$criteria object used to create the SELECT statement.
+        * @param      PropelPDO \$con
+        * @return     ".$this->getObjectClassname()."
+        * @throws     PropelException Any exceptions caught during processing will be
+        *               rethrown wrapped into a PropelException.
+        */
+       public static function doSelectOrdered(Criteria \$criteria, PropelPDO \$con = null)
+       {
+               \$critcopy = clone \$criteria;
+               \$critcopy->addAscendingOrderByColumn(".$this->getPeerClassname()."::$orderByColumn);".(isset($secondOrderByColumn) ? '
+                $critcopy->addAscendingOrderByColumn('.$this->getPeerClassname().'::'. $secondOrderByColumn.');' : '') ."
+               return ".$this->getPeerClassname()."::doSelect(\$critcopy, \$con);
+       }";
+       }
+
+       /**
         * Adds the doSelectOne() method.
         * @param      string &$script The script will be modified in this method.
         */
This entry was posted in PHP, Symfony and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>