mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 22:57:40 +08:00 
			
		
		
		
	Fixes #13467: yii\data\ActiveDataProvider no longer queries models if models count is zero
				
					
				
			This commit is contained in:
		
				
					committed by
					
						
						Alexander Makarov
					
				
			
			
				
	
			
			
			
						parent
						
							ecd2dc0d1b
						
					
				
				
					commit
					7d82bbcd37
				
			@ -18,6 +18,7 @@ Yii Framework 2 Change Log
 | 
			
		||||
- Bug #13592: Fixes Oracle’s `yii\db\oci\Schema::setTransactionIsolationLevel()` (sergeymakinen)
 | 
			
		||||
- Bug #13594: Fixes insufficient quoting in `yii\db\QueryBuilder::prepareInsertSelectSubQuery()` (sergeymakinen)
 | 
			
		||||
- Enh #13576: Added support of `srcset` to `yii\helpers\Html::img()` (Kolyunya)
 | 
			
		||||
- Enh #13467: `yii\data\ActiveDataProvider` no longer queries models if models count is zero (kLkA, Kolyunya)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
2.0.11.2 February 08, 2017
 | 
			
		||||
 | 
			
		||||
@ -104,6 +104,9 @@ class ActiveDataProvider extends BaseDataProvider
 | 
			
		||||
        $query = clone $this->query;
 | 
			
		||||
        if (($pagination = $this->getPagination()) !== false) {
 | 
			
		||||
            $pagination->totalCount = $this->getTotalCount();
 | 
			
		||||
            if ($pagination->totalCount === 0) {
 | 
			
		||||
                return [];
 | 
			
		||||
            }
 | 
			
		||||
            $query->limit($pagination->getLimit())->offset($pagination->getOffset());
 | 
			
		||||
        }
 | 
			
		||||
        if (($sort = $this->getSort()) !== false) {
 | 
			
		||||
 | 
			
		||||
@ -7,12 +7,14 @@
 | 
			
		||||
 | 
			
		||||
namespace yiiunit\framework\data;
 | 
			
		||||
 | 
			
		||||
use yii\base\InvalidCallException;
 | 
			
		||||
use yii\data\ActiveDataProvider;
 | 
			
		||||
use yii\db\Query;
 | 
			
		||||
use yiiunit\data\ar\ActiveRecord;
 | 
			
		||||
use yiiunit\data\ar\Customer;
 | 
			
		||||
use yiiunit\data\ar\Item;
 | 
			
		||||
use yiiunit\framework\db\DatabaseTestCase;
 | 
			
		||||
use yiiunit\framework\db\UnqueryableQueryMock;
 | 
			
		||||
use yiiunit\data\ar\Order;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@ -177,4 +179,23 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
 | 
			
		||||
        $provider->refresh();
 | 
			
		||||
        $this->assertEquals(2, count($provider->getModels()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDoesNotPerformQueryWhenHasNoModels()
 | 
			
		||||
    {
 | 
			
		||||
        $query = new UnqueryableQueryMock;
 | 
			
		||||
        $provider = new ActiveDataProvider([
 | 
			
		||||
            'db' => $this->getConnection(),
 | 
			
		||||
            'query' => $query->from('order')->where('0=1'),
 | 
			
		||||
        ]);
 | 
			
		||||
        $pagination = $provider->getPagination();
 | 
			
		||||
        $this->assertEquals(0, $pagination->getPageCount());
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $this->assertCount(0, $provider->getModels());
 | 
			
		||||
        } catch (InvalidCallException $exception) {
 | 
			
		||||
            $this->fail('An excessive models query was executed.');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(0, $pagination->getPageCount());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								tests/framework/db/UnqueryableQueryMock.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								tests/framework/db/UnqueryableQueryMock.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace yiiunit\framework\db;
 | 
			
		||||
 | 
			
		||||
use yii\base\InvalidCallException;
 | 
			
		||||
use yii\db\Query;
 | 
			
		||||
 | 
			
		||||
class UnqueryableQueryMock extends Query
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    public function one($db = null)
 | 
			
		||||
    {
 | 
			
		||||
        throw new InvalidCallException();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    public function all($db = null)
 | 
			
		||||
    {
 | 
			
		||||
        throw new InvalidCallException();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user