Development

Changeset 9889

You must first sign up to be able to contribute.

Changeset 9889

Show
Ignore:
Timestamp:
06/26/08 12:36:05 (5 years ago)
Author:
francois
Message:

sfPropelFinderPlugin withColumn() automatically adds missing joins

Files:

Legend:

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

    r9884 r9889  
    225225<?php 
    226226$article = sfPropelFinder::from('Article')-> 
    227   join('Category)-> 
     227  join('Category')-> 
    228228  withColumn('Category_Name')-> 
    229229  findOne(); 
     
    238238}}} 
    239239 
     240Just like `with()`, `withColumn()` will add an internal join automatically (based on the TableMap) if you don't do it yourself in the finder: 
     241{{{ 
     242#!php 
     243<?php 
     244$article = sfPropelFinder::from('Article')-> 
     245  withColumn('Category_Name')-> 
     246  findOne(); 
     247$categoryName = $article->getColumn('Category_Name');  // Works without a call to `join('Category')` 
     248}}} 
     249 
     250 
    240251This `withColumn()` method can use a column alias as second argument. 
    241252{{{ 
     
    243254<?php 
    244255$article = sfPropelFinder::from('Article')-> 
    245   join('Category)-> 
     256  join('Category')-> 
    246257  withColumn('Category_Name', 'category')-> 
    247258  findOne(); 
     
    265276<?php 
    266277$article = sfPropelFinder::from('Article')-> 
    267   join('Category)-> 
     278  join('Category')-> 
    268279  withColumn('Category_CreatedAt', 'CategoryCreatedAt', 'Timestamp')-> 
    269280  findOne(); 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9884 r9889  
    299299    foreach($this->getWithColumns() as $name => $column) 
    300300    { 
     301      // if the column is on a related object property  
     302      // and if join() wasn't called previously on this object, do a simple join 
     303      $peerClass = $column['peerClass']; 
     304      if($peerClass && !in_array($peerClass, $this->relations)) 
     305      { 
     306        list($column1, $column2) = $this->getRelation(str_replace('Peer', '', $peerClass)); 
     307        $c->addJoin($column1, $column2); 
     308        $this->relations[]= $peerClass; 
     309      } 
    301310      $c->addAsColumn($name, $column['column']); 
    302311    } 
     
    374383      } 
    375384    } 
     385    if($isCalculationColumn) 
     386    { 
     387      $peerClass = null; 
     388    } 
     389    else 
     390    { 
     391      list($peerClass, $columnName) = $this->getColName($column, null, true); 
     392    } 
    376393    $this->withColumns [$alias]= array( 
    377       'column' => $isCalculationColumn ? $column : $this->getColName($column), 
    378       'type'   => $type 
     394      'column'    => $isCalculationColumn ? $column : $columnName, 
     395      'type'      => $type, 
     396      'peerClass' => $peerClass 
    379397    ); 
    380398     
     
    669687  } 
    670688   
    671   protected function getColName($phpName, $peerClass = null
     689  protected function getColName($phpName, $peerClass = null, $withPeerClass = false
    672690  { 
    673691    if(array_key_exists($phpName, $this->withColumns)) 
     
    687705    { 
    688706      $column = call_user_func(array($peerClass, 'translateFieldName'), $phpName, BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME); 
    689       return $column; 
     707      return $withPeerClass ? array($peerClass, $column) : $column; 
    690708    } 
    691709    catch (PropelException $e) 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9884 r9889  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(99, new lime_output_color()); 
     65$t = new lime_test(100, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    568568 
    569569$comment = sfPropelFinder::from('Comment')-> 
     570  withColumn('Article_Title')-> 
     571  findOne(); 
     572$t->is($comment->getColumn('Article_Title'), 'bbbbb', 'If withColumn() is called on a related object column with no join on this class, the finder adds the join automatically'); 
     573 
     574$comment = sfPropelFinder::from('Comment')-> 
    570575  join('Article')-> 
    571576  withColumn('Article_Title', 'ArticleTitle')->