Skip to content

Commit

Permalink
Merge pull request #1354 from cebe/1102-fix-beforeFind-scopes
Browse files Browse the repository at this point in the history
need to move beforeFindInternal() after adding scopes
  • Loading branch information
cebe committed Sep 7, 2012
2 parents 650bf90 + f3e7a8f commit 05addd7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/db/ar/CActiveFinder.php
Expand Up @@ -233,9 +233,9 @@ private function buildJoinTree($parent,$with,$options=null)
$scopes=array_merge($scopes,(array)$options['scopes']); // no need for complex merging

$model->resetScope(false);
$model->beforeFindInternal();
$criteria=$model->getDbCriteria();
$criteria->scopes=$scopes;
$model->beforeFindInternal();
$model->applyScopes($criteria);
$relation->mergeWith($criteria,true);

Expand Down
25 changes: 25 additions & 0 deletions tests/framework/db/ar/CActiveRecordEventWrappersTest.php
Expand Up @@ -56,6 +56,7 @@ public function postCriteriaProvider()
array(new CDbCriteria(array('select'=>"'changedTitle' AS title")), 3, array('title'=>'changedTitle')),
array(new CDbCriteria(array('condition'=>"title='post 2'")), 1, array()),
array(new CDbCriteria(array('with'=>'comments')), 3, array()),
array(new CDbCriteria(array('scopes'=>'rename')), 3, array('title'=>'renamed post')),
);
}

Expand Down Expand Up @@ -356,6 +357,30 @@ public function testBeforeFindRelationalLazyCriteriaModification($criteria, $cou
}
}

/**
* tests if criteria modification in beforeFind() does not overide scopes defined by already applied criteria
* @dataProvider postCriteriaProviderLazy
*/
public function testBeforeFindRelationalLazyCriteriaScopes($criteria, $count, $assertations)
{
PostWithWrappers::setBeforeFindCriteria($criteria);

$user=UserWithWrappers::model()->findByPk(2);
$posts = $user->postsWithScope;
$this->assertCriteriaApplied($posts, $criteria, $count, $assertations);
foreach($posts as $post) {
$this->assertEquals('replaced content', $post->content);
}

$user=UserWithWrappers::model()->findByPk(2);
$posts = $user->posts(array('with'=>'comments','scopes'=>array('replaceContent')));
$this->assertCriteriaApplied($posts, $criteria, $count, $assertations);
foreach($posts as $post) {
$this->assertTrue($post->hasRelated('comments'));
$this->assertEquals('replaced content', $post->content);
}
}

/**
* tests number of calls to afterFind() on normal find*() method call
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/framework/db/data/models.php
Expand Up @@ -551,6 +551,7 @@ public function relations()
{
return array(
'posts'=>array(self::HAS_MANY,'PostWithWrappers','author_id'),
'postsWithScope'=>array(self::HAS_MANY,'PostWithWrappers','author_id','scopes'=>array('replaceContent')),
'postCount'=>array(self::STAT,'PostWithWrappers','author_id'),
'comments'=>array(self::HAS_MANY,'CommentWithWrappers',array('id'=>'post_id'),'through'=>'posts')
);
Expand Down Expand Up @@ -658,6 +659,18 @@ protected function afterFind()
$this->incrementCounter(__FUNCTION__);
}

public function scopes()
{
return array(
'rename'=>array(
'select'=>"'renamed post' AS title",
),
'replaceContent' => array(
'select'=>"'replaced content' AS content",
),
);
}

protected function incrementCounter($wrapper)
{
if(isset(self::$_counters[$wrapper]))
Expand Down

0 comments on commit 05addd7

Please sign in to comment.