Development

Changeset 10342

You must first sign up to be able to contribute.

Changeset 10342

Show
Ignore:
Timestamp:
07/17/08 16:17:07 (4 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added preliminary support for table aliases (from("Article a")) in Doctrine and Propel finders

Files:

Legend:

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

    r10341 r10342  
    550550}}} 
    551551 
     552=== Using Class Shortcuts === 
     553 
     554{{{ 
     555$article = sfPropelFinder::from('Article a')-> 
     556  where('a.Title', 'foo')-> 
     557  findOne(); 
     558// same as 
     559$article = sfPropelFinder::from('Article')-> 
     560  where('Article.Title', 'foo')-> 
     561  findOne(); 
     562}}} 
     563 
    552564=== Hacking the finder === 
    553565 
     
    591603=== 2008-07-17 | Trunk === 
    592604 
     605 * francois: Added preliminary support for table aliases (`from('Article a')`) in Doctrine and Propel finders 
    593606 * francois: Implemented `sfDoctrineFinder::findOne()`, `findFirst()`, `findLast()` and `orderBy()` 
    594607 * francois: Initialized `DbFinder` and `sfDoctrineFinder` (WIP) 
  • plugins/sfPropelFinderPlugin/lib/DbFinder.php

    r10213 r10342  
    1414  const  
    1515    DOCTRINE = "Doctrine", 
    16       PROPEL   = "Propel"; 
     16    PROPEL   = "Propel"; 
     17   
     18  protected $relations = array(); 
    1719   
    1820  // Finder Initializers 
     
    171173  } 
    172174   
     175  protected function getClassAndAlias($class) 
     176  { 
     177    if(strpos($class, ' ') !== false) 
     178    { 
     179      list($class, $alias) = explode(' ', $class); 
     180    } 
     181    else 
     182    { 
     183      $alias = strtolower(substr($class, 0, 1)); 
     184      while(isset($this->relations[$alias])) 
     185      { 
     186        $alias .= '1'; 
     187      } 
     188    } 
     189    return array($class, $alias); 
     190  } 
    173191} 
  • plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php

    r10341 r10342  
    1414  protected $class = null; 
    1515  protected $object = null; 
    16   protected $relations = null; 
    1716  protected $latestQuery = ''; 
    1817  protected $withClasses = array(); 
     
    2221  public function __construct($class = '') 
    2322  { 
    24     $this->query = Doctrine_Query::create()->from($class); 
     23    list($class, $alias) = $this->getClassAndAlias($class); 
     24    $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 
    2525    if($class) 
    2626    { 
    27       $this->class = $class
     27      $this->setClass($class, $alias)
    2828      $this->object = new $class(); 
    2929    } 
     
    3535  } 
    3636 
    37   public function setClass($class
    38   { 
    39     $this->relations[]= $class
     37  public function setClass($class, $alias = ''
     38  { 
     39    $this->addRelation($class, $alias)
    4040    $this->class = $class; 
    4141     
    4242    return $this; 
     43  } 
     44   
     45  protected function addRelation($class, $alias = '') 
     46  { 
     47    if(!$alias) list($class, $alias) = $this->getClassAndAlias($class); 
     48    $this->relations[$alias] = $class; 
    4349  } 
    4450   
     
    445451      } 
    446452    } 
    447      
    448     return self::underscore($phpName); 
     453    if(!array_key_exists($class, $this->relations)) 
     454    { 
     455      $relations = array_flip($this->relations); 
     456      $class = $relations[$class]; 
     457    } 
     458     
     459    return $class . '.' . self::underscore($phpName); 
    449460  } 
    450461   
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10167 r10342  
    1010 */ 
    1111  
    12 class sfPropelFinder 
     12class sfPropelFinder extends DbFinder 
    1313{ 
    1414  protected $class = null; 
     
    1616  protected $databaseMap = null; 
    1717  protected $criteria = null; 
    18   protected $relations = null; 
    1918  protected $latestQuery = ''; 
    2019  protected $criterions = array(); 
     
    2827    if($class) 
    2928    { 
     29      list($class, $alias) = $this->getClassAndAlias($class); 
    3030      $this->class = $class; 
    3131      $tmp = new $class; 
    32       $this->setPeerClass(get_class($tmp->getPeer())); 
     32      $this->setPeerClass(get_class($tmp->getPeer()), $alias); 
    3333    } 
    3434    if($this->peerClass) 
     
    5050  } 
    5151 
    52   public function setPeerClass($peerClass
    53   { 
    54     $this->relations[]= $peerClass
     52  public function setPeerClass($peerClass, $alias = ''
     53  { 
     54    $this->addRelation($peerClass, $alias)
    5555    $this->peerClass = $peerClass; 
    5656     
    5757    return $this; 
     58  } 
     59   
     60  protected function addRelation($peerClass, $alias = '') 
     61  { 
     62    if(!$alias) list($class, $alias) = $this->getClassAndAlias($peerClass); 
     63    $this->relations[$alias] = $peerClass; 
    5864  } 
    5965   
     
    9981004        // $articleFinder->join('Comment') 
    9991005        // $articleFinder->join('Category', 'RIGHT JOIN') 
    1000         $relatedClass = $args[0]
     1006        list($relatedClass, $alias) = $this->getClassAndAlias($args[0])
    10011007        list($column1, $column2) = $this->getRelation($relatedClass); 
    1002         $this->relations[]= sfPropelFinderUtils::getPeerClassFromClass($relatedClass); 
     1008        $this->addRelation(sfPropelFinderUtils::getPeerClassFromClass($relatedClass), $alias); 
    10031009        $operator = isset($args[1]) ? $args[1] : null; 
    10041010        break; 
     
    10091015        if($peerClass1 != $this->peerClass && !$this->hasRelation($peerClass1)) 
    10101016        { 
    1011           $this->relations []= $peerClass1; 
     1017          list($peerClass1, $alias) = $this->getClassAndAlias($peerClass1); 
     1018          $this->addRelation($peerClass1, $alias); 
    10121019        } 
    10131020        list($peerClass2, $column2) = $this->getColName($column2, $peerClass = null, $withPeerClass = true, $autoAddJoin = false); 
    10141021        if($peerClass2 != $this->peerClass && !$this->hasRelation($peerClass2)) 
    10151022        { 
    1016           $this->relations []= $peerClass2; 
     1023          list($peerClass2, $alias) = $this->getClassAndAlias($peerClass2); 
     1024          $this->addRelation($peerClass2, $alias); 
    10171025        } 
    10181026        break; 
     
    11191127      // Table.Column 
    11201128      list($class, $phpName) = explode('.', $phpName); 
    1121       $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 
     1129      if(array_key_exists($class, $this->relations)) 
     1130      { 
     1131        $peerClass = $this->relations[$class]; 
     1132      } 
     1133      else 
     1134      { 
     1135        $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 
     1136      } 
    11221137    } 
    11231138    else if(strpos($phpName, '_') !== false) 
  • plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php

    r10341 r10342  
    6060$databaseManager->initialize(); 
    6161 
    62 $t = new lime_test(3, new lime_output_color()); 
     62$t = new lime_test(5, new lime_output_color()); 
    6363 
    6464$t->diag('getColName()'); 
     
    7272} 
    7373$finder = new myFinder('Article'); 
    74 $t->is($finder->getColName('Title'), 'title', 'getColName() recognizes [column phpName]'); 
    75 $t->is($finder->getColName('Article_Title'), 'title', 'getColName() recognizes [table phpName]_[column phpName]'); 
    76 $t->is($finder->getColName('Article.Title'), 'title', 'getColName() recognizes [table phpName].[column phpName]'); 
     74$t->is($finder->getColName('Title'), 'a.title', 'getColName() recognizes [column phpName]'); 
     75$t->is($finder->getColName('Article_Title'), 'a.title', 'getColName() recognizes [table phpName]_[column phpName]'); 
     76$t->is($finder->getColName('Article.Title'), 'a.title', 'getColName() recognizes [table phpName].[column phpName]'); 
     77$t->is($finder->getColName('a.Title'), 'a.title', 'getColName() recognizes [table alias].[column phpName]'); 
     78 
     79$finder = new myFinder('Article b'); 
     80$t->is($finder->getColName('b.Title'), 'b.title', 'getColName() recognizes [table alias].[column phpName]'); 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderInternalsTest.php

    r10121 r10342  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(3, new lime_output_color()); 
     67$t = new lime_test(5, new lime_output_color()); 
    6868 
    6969$t->diag('getColName()'); 
     
    8080$t->is($finder->getColName('Article_Title'), 'article.TITLE', 'getColName() recognizes [table phpName]_[column phpName]'); 
    8181$t->is($finder->getColName('Article.Title'), 'article.TITLE', 'getColName() recognizes [table phpName].[column phpName]'); 
     82 
     83$t->is($finder->getColName('a.Title'), 'article.TITLE', 'getColName() recognizes [table alias].[column phpName]'); 
     84 
     85$finder = new myFinder('Article b'); 
     86$t->is($finder->getColName('b.Title'), 'article.TITLE', 'getColName() recognizes [table alias].[column phpName]'); 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderRelationsTest.php

    r10167 r10342  
    101101} 
    102102 
    103  
    104103ClubPeer::doDeleteAll(); 
    105104PersonPeer::doDeleteAll(); 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10166 r10342  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(113, new lime_output_color()); 
     67$t = new lime_test(115, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    399399$article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 
    400400$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 
     401$article = sfPropelFinder::from('Article')->where('a.Title', 'abc')->findOne(); 
     402$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassShortcut.ColumnName'); 
     403$article = sfPropelFinder::from('Article b')->where('b.Title', 'abc')->findOne(); 
     404$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassAlias.ColumnName'); 
    401405$article = sfPropelFinder::from('Article')->where('Title', 'def')->findOne(); 
    402406$t->is($article->getId(), $article2->getId(), 'where() adds a WHERE condition on the column given as first argument');