Development

Changeset 10755

You must first sign up to be able to contribute.

Changeset 10755

Show
Ignore:
Timestamp:
08/08/08 18:35:14 (4 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Implemented sfDoctrineFinder::delete()

Files:

Legend:

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

    r10670 r10755  
    531531--------- 
    532532 
    533 ### 2008-08-05 | Trunk 
    534  
     533### 2008-08-08 | Trunk 
     534 
     535* francois: Implemented `sfDoctrineFinder::delete()` 
    535536* francois: Turned README into Markdown syntax, and changed the main name to `DbFinder` 
    536537* francois: `DbFinder::from('Article')` returns an instance of `ArticleFinder` if it exists. That way, extending the finder gets easier. 
  • plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php

    r10666 r10755  
    309309  public function delete($forceIndividualDeletes = false) 
    310310  { 
     311    if($forceIndividualDeletes) 
     312    { 
     313      $objects = $this->query->delete()->execute(); 
     314      $nbDeleted = 0; 
     315      foreach($objects as $object) 
     316      { 
     317        $object->delete(); 
     318        $nbDeleted++; 
     319      } 
     320    } 
     321    else 
     322    { 
     323      if (!$this->query->contains('where')) 
     324      { 
     325        // Empty queries don't return the number of deleted rows 
     326        $this->query->addWhere('1 = 1'); 
     327      } 
     328      $nbDeleted = $this->query->delete()->execute(); 
     329    } 
    311330    $this->cleanup(); 
    312     throw new Exception('This method is not yet implemented'); 
     331     
     332    return $nbDeleted; 
    313333  } 
    314334   
     
    515535    else 
    516536    { 
    517       $this->query->addWhere(sprintf('%s %s ?', $column, $comparison), is_array($value) ? $value : array($value)); 
     537      if(is_null($value)) 
     538      { 
     539        $this->query->addWhere(sprintf('%s %s', $column, $comparison)); 
     540      } 
     541      else 
     542      { 
     543        $this->query->addWhere(sprintf('%s %s ?', $column, $comparison), is_array($value) ? $value : array($value)); 
     544      } 
    518545    } 
    519546  } 
  • plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php

    r10655 r10755  
    2727    DArticleI18n: 
    2828      columns: 
     29        id:          { type: integer, primary: true } 
     30        culture:     { type: string(5), primary: true } 
    2931        content:     string(255) 
    3032    DCategory: 
     
    7173Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
    7274 
    73 $t = new lime_test(53, new lime_output_color()); 
     75$t = new lime_test(77, new lime_output_color()); 
    7476 
    7577$t->diag('find()'); 
     
    353355$nbArticles = $finder->whereTitle('foo2')->count(); 
    354356$t->is($nbArticles, 1, 'count() returns the number of records matching the condition'); 
     357 
     358$t->diag('delete()'); 
     359 
     360Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
     361$finder = new sfDoctrineFinder('DArticle'); 
     362$nbDeleted = $finder->delete(); 
     363$t->is($nbDeleted, 0, 'delete() on an empty finder with no results returns 0'); 
     364$article2 = new DArticle(); 
     365$article2->setTitle('foo2'); 
     366$article2->save(); 
     367$article3 = new DArticle(); 
     368$article3->setTitle('foo3'); 
     369$article3->save(); 
     370$finder = new sfDoctrineFinder('DArticle'); 
     371$nbDeleted = $finder->delete(); 
     372$t->is($nbDeleted, 2, 'delete() deletes records from the table and returns the number of deleted rows'); 
     373$finder = new sfDoctrineFinder('DArticle'); 
     374$nbArticles = $finder->count(); 
     375$t->is($nbArticles, 0, 'delete() on an empty finder deletes all rows'); 
     376$article2 = new DArticle(); 
     377$article2->setTitle('foo2'); 
     378$article2->save(); 
     379$article3 = new DArticle(); 
     380$article3->setTitle('foo3'); 
     381$article3->save(); 
     382$finder = new sfDoctrineFinder('DArticle'); 
     383$nbDeleted = $finder->where('Title', 'foo2')->delete(); 
     384$t->is($nbDeleted, 1, 'delete() deletes all rows found by a finder'); 
     385$nbArticles = $finder->count(); 
     386$t->is($nbArticles, 1, 'delete() does not delete rows not found by the finder'); 
     387 
     388$t->diag('where()'); 
     389 
     390Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
     391$article1 = new DArticle(); 
     392$article1->setTitle('abc'); 
     393$article1->save(); 
     394$article2 = new DArticle(); 
     395$article2->setTitle('def'); 
     396$article2->save(); 
     397$article3 = new DArticle(); 
     398$article3->setTitle('bbc'); 
     399$article3->save(); 
     400$article = sfDoctrineFinder::from('DArticle')->where('Title', 'abc')->findOne(); 
     401$t->is($article->getId(), $article1->getId(), 'where() accepts a simple CamelCase column name like ClassName'); 
     402try 
     403{ 
     404  $article = sfDoctrineFinder::from('DArticle')->where('Foo', 'abc')->find(); 
     405  $t->fail('where() throws an exception when the column is not found (but only after calling find())'); 
     406} 
     407catch (Exception $e) 
     408{ 
     409  $t->pass('where() throws an exception when the column is not found (but only after calling find())'); 
     410} 
     411$article = sfDoctrineFinder::from('DArticle')->where('DArticle.Title', 'abc')->findOne(); 
     412$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName.ColumnName'); 
     413$article = sfDoctrineFinder::from('DArticle')->where('DArticle_Title', 'abc')->findOne(); 
     414$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 
     415$article = sfDoctrineFinder::from('DArticle')->where('d.Title', 'abc')->findOne(); 
     416$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassShortcut.ColumnName'); 
     417$article = sfDoctrineFinder::from('DArticle b')->where('b.Title', 'abc')->findOne(); 
     418$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassAlias.ColumnName'); 
     419$article = sfDoctrineFinder::from('DArticle')->where('Title', 'def')->findOne(); 
     420$t->is($article->getId(), $article2->getId(), 'where() adds a WHERE condition on the column given as first argument'); 
     421$articles = sfDoctrineFinder::from('DArticle')->where('Title', ' LIKE ', '%bc')->find(); 
     422$t->is(count($articles), 2, 'where() accepts a comparator as second argument when three arguments are given'); 
     423$articles = sfDoctrineFinder::from('DArticle')->where('Title', 'like', '%bc')->find(); 
     424$t->is(count($articles), 2, 'where() accepts a text comparator and is permissive on syntax'); 
     425$articles = sfDoctrineFinder::from('DArticle')->where('Title', 'is not null', null)->find(); 
     426$t->is(count($articles), 3, 'where() accepts a text comparator and is permissive on syntax'); 
     427$articles = sfDoctrineFinder::from('DArticle')->where('Title', ' in ', array('abc', 'def'))->find(); 
     428$t->is(count($articles), 2, 'where() accepts a "in" comparator'); 
     429 
     430try 
     431{ 
     432  $article = sfDoctrineFinder::from('DArticle')->whereFoo('abc')->find(); 
     433  $t->fail('whereXXX() throws an exception when the XXX column is not found (but only after calling find())'); 
     434} 
     435catch (Exception $e) 
     436{ 
     437  $t->pass('whereXXX() throws an exception when the XXX column is not found (but only after calling find())'); 
     438} 
     439 
     440try 
     441{ 
     442  $article = sfDoctrineFinder::from('DArticle')->wherecategoryid('abc')->find(); 
     443  $t->fail('whereXXX() expects a column name in CamelCase'); 
     444} 
     445catch (Exception $e) 
     446{ 
     447  $t->pass('whereXXX() expects a column name in CamelCase'); 
     448} 
     449 
     450try 
     451{ 
     452  $article = sfDoctrineFinder::from('DArticle')->whereCategoryId('abc')->find(); 
     453  $t->pass('whereXXX() expects a column name in CamelCase'); 
     454} 
     455catch (Exception $e) 
     456{ 
     457  $t->fail('whereXXX() expects a column name in CamelCase'); 
     458} 
     459 
     460try 
     461{ 
     462  $article = sfDoctrineFinder::from('DArticle')->whereTitle('abc', 'def', 'ghi', 'jkl')->find(); 
     463  $t->fail('whereXXX() throws an exception when called with more than three parameters'); 
     464} 
     465catch (Exception $e) 
     466{ 
     467  $t->pass('whereXXX() throws an exception when called with more than three parameters'); 
     468} 
     469 
     470$article = sfDoctrineFinder::from('DArticle')->whereTitle('abc')->findOne(); 
     471$t->is($article->getId(), $article1->getId(), 'whereXXX() adds a where condition according to the XXX column name'); 
     472$article = sfDoctrineFinder::from('DArticle')->whereDArticle_Title('abc')->findOne(); 
     473$t->is($article->getId(), $article1->getId(), 'whereXXX() accepts a complete column name like whereClassName_ColumnName()'); 
     474$article = sfDoctrineFinder::from('DArticle')->whereTitle('def')->findOne(); 
     475$t->is($article->getId(), $article2->getId(), 'whereXXX() adds a WHERE condition on the XXX column'); 
     476$articles = sfDoctrineFinder::from('DArticle')->whereTitle('like', '%bc')->find(); 
     477$t->is(count($articles), 2, 'whereXXX() accepts a comparator as first argument when two arguments are given'); 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10655 r10755  
    422422try 
    423423{ 
    424   $article = sfPropelFinder::from('Article')->wheretitle('abc'); 
     424  $article = sfPropelFinder::from('Article')->wherecategoryid('abc'); 
    425425  $t->fail('whereXXX() expects a column name in CamelCase'); 
    426426} 
     
    432432try 
    433433{ 
    434   $article = sfPropelFinder::from('Article')->whereTitle('abc'); 
     434  $article = sfPropelFinder::from('Article')->whereCategoryId('abc'); 
    435435  $t->pass('whereXXX() expects a column name in CamelCase'); 
    436436}