mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fixes #16891: Fixed Pagination::totalCount initialized incorrectly
This commit is contained in:
		
				
					committed by
					
						
						Alexander Makarov
					
				
			
			
				
	
			
			
			
						parent
						
							379a2002dd
						
					
				
				
					commit
					e1623868f9
				
			@ -4,6 +4,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
2.0.16 under development
 | 
					2.0.16 under development
 | 
				
			||||||
------------------------
 | 
					------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- Bug #16891: Fixed Pagination::totalCount initialized incorrectly (taobig)
 | 
				
			||||||
- Bug #16028: Fix serialization of complex cache keys that contain non-UTF sequences (rugabarbo)
 | 
					- Bug #16028: Fix serialization of complex cache keys that contain non-UTF sequences (rugabarbo)
 | 
				
			||||||
- Bug #16945: Fixed RBAC DbManager ruleName fetching on the case of PDO::ATTR_ORACLE_NULLS => PDO::NULL_TO_STRING (razonyang)
 | 
					- Bug #16945: Fixed RBAC DbManager ruleName fetching on the case of PDO::ATTR_ORACLE_NULLS => PDO::NULL_TO_STRING (razonyang)
 | 
				
			||||||
- Bug #16081: Fixed composite IN using just one column (rugabarbo)
 | 
					- Bug #16081: Fixed composite IN using just one column (rugabarbo)
 | 
				
			||||||
 | 
				
			|||||||
@ -46,6 +46,9 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
 | 
				
			|||||||
    public $id;
 | 
					    public $id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private $_sort;
 | 
					    private $_sort;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var Pagination|false $_pagination
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    private $_pagination;
 | 
					    private $_pagination;
 | 
				
			||||||
    private $_keys;
 | 
					    private $_keys;
 | 
				
			||||||
    private $_models;
 | 
					    private $_models;
 | 
				
			||||||
@ -183,8 +186,6 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Returns the pagination object used by this data provider.
 | 
					     * Returns the pagination object used by this data provider.
 | 
				
			||||||
     * Note that you should call [[prepare()]] or [[getModels()]] first to get correct values
 | 
					 | 
				
			||||||
     * of [[Pagination::totalCount]] and [[Pagination::pageCount]].
 | 
					 | 
				
			||||||
     * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled.
 | 
					     * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function getPagination()
 | 
					    public function getPagination()
 | 
				
			||||||
@ -193,6 +194,13 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
 | 
				
			|||||||
            $this->setPagination([]);
 | 
					            $this->setPagination([]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (($this->_pagination !== false) && ($this->_pagination->totalCount === null)) {
 | 
				
			||||||
 | 
					            if ($this->_totalCount === null) {
 | 
				
			||||||
 | 
					                $this->setTotalCount($this->prepareTotalCount());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            $this->_pagination->totalCount = $this->_totalCount;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->_pagination;
 | 
					        return $this->_pagination;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -124,9 +124,9 @@ class Pagination extends BaseObject implements Linkable
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public $validatePage = true;
 | 
					    public $validatePage = true;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @var int total number of items.
 | 
					     * @var int|null total number of items.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public $totalCount = 0;
 | 
					    public $totalCount;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @var int the default page size. This property will be returned by [[pageSize]] when page size
 | 
					     * @var int the default page size. This property will be returned by [[pageSize]] when page size
 | 
				
			||||||
     * cannot be determined by [[pageSizeParam]] from [[params]].
 | 
					     * cannot be determined by [[pageSizeParam]] from [[params]].
 | 
				
			||||||
 | 
				
			|||||||
@ -170,7 +170,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
 | 
				
			|||||||
            'query' => $query->from('order')->orderBy('id'),
 | 
					            'query' => $query->from('order')->orderBy('id'),
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
        $pagination = $provider->getPagination();
 | 
					        $pagination = $provider->getPagination();
 | 
				
			||||||
        $this->assertEquals(0, $pagination->getPageCount());
 | 
					        $this->assertEquals(1, $pagination->getPageCount());
 | 
				
			||||||
        $this->assertCount(3, $provider->getModels());
 | 
					        $this->assertCount(3, $provider->getModels());
 | 
				
			||||||
        $this->assertEquals(1, $pagination->getPageCount());
 | 
					        $this->assertEquals(1, $pagination->getPageCount());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -198,4 +198,45 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(0, $pagination->getPageCount());
 | 
					        $this->assertEquals(0, $pagination->getPageCount());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function testValidateTotalCount()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $provider = new ActiveDataProvider([
 | 
				
			||||||
 | 
					            'query' => Order::find()->orderBy('id'),
 | 
				
			||||||
 | 
					            'totalCount' => 100,
 | 
				
			||||||
 | 
					            'pagination' => [
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					        $this->assertSame(100, $provider->pagination->totalCount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $provider = new ActiveDataProvider([
 | 
				
			||||||
 | 
					            'query' => Order::find()->orderBy('id'),
 | 
				
			||||||
 | 
					            'totalCount' => 100,
 | 
				
			||||||
 | 
					            'pagination' => false,
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					        $this->assertSame(false, $provider->pagination);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function testValidatePageCount()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $provider = new ActiveDataProvider([
 | 
				
			||||||
 | 
					            'query' => Order::find()->orderBy('id'),
 | 
				
			||||||
 | 
					            'totalCount' => 100,
 | 
				
			||||||
 | 
					            'pagination' => [
 | 
				
			||||||
 | 
					                'pageSize' => 101,
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					        $this->assertSame(1, $provider->pagination->pageCount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $provider = new ActiveDataProvider([
 | 
				
			||||||
 | 
					            'query' => Order::find()->orderBy('id'),
 | 
				
			||||||
 | 
					            'totalCount' => 100,
 | 
				
			||||||
 | 
					            'pagination' => [
 | 
				
			||||||
 | 
					                'pageSize' => 1,
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					        $this->assertSame(100, $provider->pagination->pageCount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user