Development

Changeset 8129

You must first sign up to be able to contribute.

Changeset 8129

Show
Ignore:
Timestamp:
03/28/08 00:12:06 (7 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Refactored main class for better readability

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelFinderPlugin/README

    r8119 r8129  
    196196 
    197197 * Allow hydrating of related objects (doSelectJoinXXX equivalent) 
    198  * Add more magic! 
    199  * Merge with sfPropelImpersonatorPlugin? 
     198 * Add And 
     199 * Add Or 
     200 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 
     201 * Merge with sfPropelImpersonatorPlugin! 
    200202 
    201203== Changelog == 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r8119 r8129  
    1717  protected $relations = null; 
    1818   
    19   public function getPeerClass() 
    20   { 
    21     return $this->peerClass; 
    22   } 
    23  
    24   public function setPeerClass($peerClass) 
    25   { 
    26     $this->relations[]= $peerClass; 
    27     return $this->peerClass = $peerClass; 
    28   } 
    29    
    3019  public function __construct($class = '') 
    3120  { 
     
    4837  } 
    4938   
     39  public function getPeerClass() 
     40  { 
     41    return $this->peerClass; 
     42  } 
     43 
     44  public function setPeerClass($peerClass) 
     45  { 
     46    $this->relations[]= $peerClass; 
     47    $this->peerClass = $peerClass; 
     48    return $this; 
     49  } 
     50   
     51  public function getCriteria() 
     52  { 
     53    return $this->criteria; 
     54  } 
     55   
     56  public function setCriteria($criteria) 
     57  { 
     58    $this->criteria = $criteria; 
     59    return $this; 
     60  } 
     61 
    5062  public function reinitCriteria() 
    5163  { 
    5264    $this->criteria = new Criteria(); 
    53   } 
     65    return $this; 
     66  } 
     67   
     68  // Finder Initializers 
    5469   
    5570  public static function from($class) 
     
    6075    return $finder; 
    6176  } 
     77   
     78  // Finder Executers 
    6279   
    6380  public function count($con = null, $reinitCriteria = true) 
     
    130147  } 
    131148   
     149  // Finder Filters 
     150   
     151  /** 
     152   * Finder Fluid Interface for Criteria::setDistinct() 
     153   */ 
    132154  public function distinct() 
    133155  { 
     
    137159  } 
    138160   
     161  /** 
     162   * Finder Fluid Interface for Criteria::add() 
     163   */ 
    139164  public function where($column, $value, $comparison = Criteria::EQUAL) 
    140165  { 
     
    143168    return $this; 
    144169  } 
    145  
     170   
     171  /** 
     172   * Magic version of where() 
     173   * Infers $column, $value, $comparison from $columnName and some optional arguments 
     174   * Examples: 
     175   *   $articleFinder->_where('CommentId', 3) 
     176   *    => $c->add(ArticlePeer::COMMENT_ID, 3) 
     177   *   $articleFinder->_where('Title', 'like', '%foo') 
     178   *    => $c->add(ArticlePeer::TITLE, '%foo', Criteria::LIKE) 
     179   */ 
     180  public function _where($columnName, $arguments = array()) 
     181  { 
     182    $column = $this->getColName($columnName); 
     183    $comparison = Criteria::EQUAL; 
     184    switch (count($arguments)) 
     185    { 
     186      case 0: 
     187        $value = true; 
     188        break; 
     189      case 1: 
     190        $value = array_shift($arguments); 
     191        break; 
     192      case 2: 
     193        $comparison = array_shift($arguments); 
     194        $comparisonUp = trim(strtoupper($comparison)); 
     195        if(in_array($comparisonUp, array('LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL'))) 
     196        { 
     197          $comparison = ' '.$comparisonUp.' '; 
     198        } 
     199        $value = array_shift($arguments); 
     200        break; 
     201      default: 
     202        throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments'); 
     203    } 
     204     
     205    return $this->where($column, $value, $comparison); 
     206  } 
     207 
     208  /** 
     209   * Finder Fluid Interface for Criteria::addAscendingOrderByColumn() 
     210   * and Criteria::addDescendingOrderByColumn() 
     211   */ 
    146212  public function orderBy($column, $order = Criteria::ASC) 
    147213  { 
     
    161227  } 
    162228   
     229  /** 
     230   * Magic version of orderBy() 
     231   * Infers $column and $order from $columnName and some optional arguments 
     232   * Examples: 
     233   *   $articleFinder->_orderBy('CreatedAt') 
     234   *    => $c->addAscendingOrderByColumn(ArticlePeer::CREATED_AT) 
     235   *   $articlefinder->_orderBy('CategoryId', 'desc') 
     236   *    => $c->addDescendingOrderByColumn(ArticlePeer::CATEGORY_ID) 
     237   */ 
     238  public function _orderBy($columnName, $arguments = array()) 
     239  { 
     240    $column = $this->getColName($columnName); 
     241    $order = strtoupper(array_shift($arguments)); 
     242    if(!$order) 
     243    { 
     244      $order = Criteria::ASC; 
     245    } 
     246     
     247    return $this->orderBy($column, $order); 
     248  } 
     249   
     250  /** 
     251   * Finder Fluid Interface for Criteria::addJoin() 
     252   */ 
    163253  public function join($column1, $column2, $operator = null) 
    164254  { 
     
    168258  } 
    169259   
    170   public function __call($name, $arguments) 
    171   { 
    172     // simple where equal 
    173     if(strpos($name, 'where') === 0) 
    174     { 
    175       $columnName = substr($name, 5); 
    176       if(strpos($columnName, '_') !== false) 
    177       { 
    178         list($class, $columnName) = split('_', $columnName); 
    179         $column = $this->getColName($columnName, $class.'Peer'); 
    180       } 
    181       else 
    182       { 
    183         $column = $this->getColName($columnName); 
    184       } 
    185       $comparison = Criteria::EQUAL; 
    186       switch (count($arguments)) 
    187       { 
    188         case 0: 
    189           $value = true; 
    190           break; 
    191         case 1: 
    192           $value = array_shift($arguments); 
    193           break; 
    194         case 2: 
    195           $comparison = array_shift($arguments); 
    196           $comparisonUp = trim(strtoupper($comparison)); 
    197           if(in_array($comparisonUp, array('LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL'))) 
    198           { 
    199             $comparison = ' '.$comparisonUp.' '; 
    200           } 
    201           $value = array_shift($arguments); 
    202           break; 
    203         default: 
    204           throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments'); 
    205       } 
    206       return $this->where($column, $value, $comparison); 
    207     } 
    208      
    209     // order by column 
    210     if(strpos($name, 'orderBy') === 0) 
    211     { 
    212       $column = $this->getColName(substr($name, 7)); 
    213       $order = strtoupper(array_shift($arguments)); 
    214       if(!$order) 
    215       { 
    216         $order = Criteria::ASC; 
    217       } 
    218       return $this->orderBy($column, $order); 
    219     } 
    220      
    221     // join 
    222     if(strpos($name, 'join') === 0) 
    223     { 
    224       $relatedClass = substr($name, 4); 
    225       list($column1, $column2) = $this->getRelation($relatedClass); 
    226       $this->relations[]= $relatedClass.'Peer'; 
    227       $operator = array_shift($arguments); 
    228       if(!$operator) 
    229       { 
    230         $operator = null; 
    231       } 
    232       return $this->join($column1, $column2, $operator); 
    233     } 
    234      
    235     return $this; 
    236   } 
    237    
     260  /** 
     261   * Magic version of join() 
     262   * Infers $column1, $column2 and $operator from $relatedClass and some optional arguments 
     263   * Uses the Propel column maps, based on the schema, to guess the related columns 
     264   * Examples: 
     265   *   $articleFinder->_join('Comment') 
     266   *    => $c->addJoin(ArticlePeer::ID, CommentPeer::ARTICLE_ID) 
     267   *   $articleFinder->_join('Category', 'RIGHT JOIN') 
     268   *    => $c->addJoin(ArticlePeer::CATEGORY_ID, CategoryPeer::ID, Criteria::RIGHT_JOIN) 
     269   */ 
     270  public function _join($relatedClass, $arguments = array()) 
     271  { 
     272    list($column1, $column2) = $this->getRelation($relatedClass); 
     273    $this->relations[]= $relatedClass.'Peer'; 
     274    $operator = array_shift($arguments); 
     275    if(!$operator) 
     276    { 
     277      $operator = null; 
     278    } 
     279     
     280    return $this->join($column1, $column2, $operator); 
     281  } 
     282 
    238283  public function getRelation($phpName) 
    239284  { 
     
    285330  protected function getColName($phpName, $peerClass = null) 
    286331  { 
     332    if(strpos($phpName, '_') !== false) 
     333    { 
     334      list($class, $phpName) = split('_', $phpName); 
     335      $peerClass = $class.'Peer'; 
     336    } 
    287337    if(!$peerClass) 
    288338    { 
     
    299349    } 
    300350  } 
     351   
     352  public function __call($name, $arguments) 
     353  { 
     354    if(strpos($name, 'where') === 0) 
     355    { 
     356      return $this->_where(substr($name, 5), $arguments); 
     357    } 
     358    if(strpos($name, 'orderBy') === 0) 
     359    { 
     360      return $this->_orderBy(substr($name, 7), $arguments); 
     361    } 
     362    if(strpos($name, 'join') === 0) 
     363    { 
     364      return $this->_join(substr($name, 4), $arguments); 
     365    } 
     366    throw new Exception(sprintf('{sfPropelFinder} Undefined method %s', $name)); 
     367  } 
    301368}