Development

Changeset 28805

You must first sign up to be able to contribute.

Changeset 28805

Show
Ignore:
Timestamp:
03/26/10 16:57:03 (3 years ago)
Author:
Leon.van.der.Ree
Message:

fixed bug when joining deeper objectPaths
added support for propertyPaths when sorting and filtering

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelObjectPathBehaviorPlugin/branches/1.5/lib/ObjectPathCriteria.php

    r28754 r28805  
    1515   *  
    1616   * @param     string $objectPath  one or more strings with objectPaths 
    17    * @return    string 
     17   * @return    ObjectPathCriteria  fluid interface 
    1818   */ 
    1919  public function joinByObjectPath($objectPath) 
     
    3737      // don't perform JoinWith, if already joined 
    3838      // alternatively I could test if the join is not already in the with-array  
    39       // (this is a little more computational intense, but will perform with even though the join has already been performed)  
    40       if (!isset($this->joins[$alias])) 
     39      // (this is a little more computational intense, but will perform with even though the join has already been performed) 
     40      $newJoin = !isset($this->joins[$alias]); 
     41      if ($newJoin) 
    4142      { 
    4243        $this->joinWith($relationName.' '.$alias); 
     
    4647      if (isset($relationsNames[1])) 
    4748      { 
    48           $this->useQuery($alias) 
    49             ->joinByObjectPath($relationsNames[1]) 
    50           ->endUse(); 
     49        $related = $this->useQuery($alias) 
     50          ->joinByObjectPath($relationsNames[1]); 
     51         
     52        // merge down joined relation, if new 
     53        if ($newJoin) 
     54        { 
     55          $related->endUse(); 
     56        } 
     57 
    5158      } 
    5259    } 
     
    124131       
    125132      if (isset($this->joins[$alias])) { 
    126         return $this->useQuery($alias)->getColumnFromName($columnName, $failSilently); 
     133        return $this->useQuery($alias) 
     134          ->getColumnFromName($columnName, $failSilently); 
    127135      } 
    128136       
     
    132140    // process normal column as before 
    133141    return parent::getColumnFromName($propertyPath, $failSilently); 
    134   }   
     142  } 
     143   
     144  /** 
     145   * Adds a condition on a column based on a propertyPath and a value 
     146   * Uses introspection to translate the propertyPath into a fully qualified name 
     147   * <code> 
     148   * $c->filterBy('Relation.Title', 'table_alias.foo'); 
     149   * </code> 
     150   * 
     151   * @see        Criteria::add() 
     152   *  
     153   * @param      string $column     A string representing thecolumn phpName, e.g. 'AuthorId' 
     154   * @param      mixed  $value      A value for the condition 
     155   * @param      string $comparison What to use for the column comparison, defaults to Criteria::EQUAL 
     156   * 
     157   * @return     ModelCriteria The current object, for fluid interface 
     158   */ 
     159  public function filterBy($column, $value, $comparison = Criteria::EQUAL) 
     160  { 
     161    // check if an objectPath has been given 
     162    $lastDot = strrpos($column, '.'); 
     163    if ($lastDot !== false) 
     164    { 
     165      // get the objectPath 
     166      $objectPath = substr($column, 0, $lastDot); 
     167       
     168      // and get Related Query Class 
     169      $strRelated = $this->translateObjectPathToAlias($objectPath); 
     170      $column = $strRelated.'.'.substr($column, $lastDot + 1); 
     171       
     172      // TODO: this can be removed when filterBy is patched in propel 
     173      return $this->useQuery($strRelated) 
     174        ->filterBy($column, $value, $comparison) 
     175      ->endUse();; 
     176    } 
     177     
     178    return parent::filterBy($column, $value, $comparison); 
     179  } 
     180   
     181   
     182  /** 
     183   * Adds an ORDER BY clause to the query 
     184   * Usability layer on top of Criteria::addAscendingOrderByColumn() and Criteria::addDescendingOrderByColumn() 
     185   * Infers $column and $order from $columnName and some optional arguments 
     186   * Examples: 
     187   *   $c->orderBy('Book.CreatedAt') 
     188   *    => $c->addAscendingOrderByColumn(BookPeer::CREATED_AT) 
     189   *   $c->orderBy('Book.CategoryId', 'desc') 
     190   *    => $c->addDescendingOrderByColumn(BookPeer::CATEGORY_ID) 
     191   * 
     192   * @param string $columnName The column to order by 
     193   * @param string $order      The sorting order. Criteria::ASC by default, also accepts Criteria::DESC 
     194   * 
     195   * @return     ModelCriteria The current object, for fluid interface 
     196   */ 
     197  public function orderBy($columnName, $order = Criteria::ASC) 
     198  { 
     199    // check if an objectPath has been given 
     200    $lastDot = strrpos($columnName, '.'); 
     201    if ($lastDot !== false) 
     202    { 
     203      // get the objectPath 
     204      $objectPath = substr($columnName, 0, $lastDot); 
     205       
     206      // and get Related Query Class 
     207      $strRelated = $this->translateObjectPathToAlias($objectPath); 
     208      $columnName = $strRelated.'.'.substr($columnName, $lastDot + 1); 
     209       
     210//      return $this->useQuery($strRelated) 
     211//        ->orderBy($columnName, $order) 
     212//      ->endUse();; 
     213    } 
     214     
     215    return parent::orderBy($columnName, $order); 
     216  }  
    135217 
    136218}