mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
#18475: Correct conditional configuration of db property
This commit is contained in:
@ -56,12 +56,11 @@ use yii\di\Instance;
|
||||
class ActiveDataProvider extends BaseDataProvider
|
||||
{
|
||||
/**
|
||||
* @var QueryInterface the query that is used to fetch data models and [[totalCount]]
|
||||
* if it is not explicitly set.
|
||||
* @var QueryInterface|null the query that is used to fetch data models and [[totalCount]] if it is not explicitly set.
|
||||
*/
|
||||
public $query;
|
||||
/**
|
||||
* @var string|callable the column that is used as the key of the data models.
|
||||
* @var string|callable|null the column that is used as the key of the data models.
|
||||
* This can be either a column name, or a callable that returns the key value of a given data model.
|
||||
*
|
||||
* If this is not set, the following rules will be used to determine the keys of the data models:
|
||||
@ -73,8 +72,8 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
*/
|
||||
public $key;
|
||||
/**
|
||||
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
|
||||
* If not set, the default DB connection will be used.
|
||||
* @var Connection|array|string|null the DB connection object or the application component ID of the DB connection.
|
||||
* If set it overrides [[query]] default DB connection.
|
||||
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
|
||||
*/
|
||||
public $db;
|
||||
@ -82,13 +81,15 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
|
||||
/**
|
||||
* Initializes the DB connection component.
|
||||
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
|
||||
* This method will initialize the [[db]] property (when set) to make sure it refers to a valid DB connection.
|
||||
* @throws InvalidConfigException if [[db]] is invalid.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$this->db = Instance::ensure($this->db === null ? 'db' : $this->db, Connection::className());
|
||||
if ($this->db !== null) {
|
||||
$this->db = Instance::ensure($this->db, Connection::className());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -173,7 +174,7 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
public function setSort($value)
|
||||
{
|
||||
parent::setSort($value);
|
||||
if (($sort = $this->getSort()) !== false && $this->query instanceof ActiveQueryInterface) {
|
||||
if ($this->query instanceof ActiveQueryInterface && ($sort = $this->getSort()) !== false) {
|
||||
/* @var $modelClass Model */
|
||||
$modelClass = $this->query->modelClass;
|
||||
$model = $modelClass::instance();
|
||||
|
@ -8,21 +8,16 @@
|
||||
namespace yiiunit\framework\data;
|
||||
|
||||
use yii\data\ActiveDataProvider;
|
||||
use yii\db\Connection;
|
||||
use yii\db\Query;
|
||||
use yiiunit\TestCase;
|
||||
|
||||
class ActiveDataProviderCloningTest extends TestCase
|
||||
{
|
||||
|
||||
// Tests :
|
||||
|
||||
public function testClone()
|
||||
{
|
||||
$queryFirst = new Query();
|
||||
|
||||
$dataProviderFirst = new ActiveDataProvider([
|
||||
'db' => new Connection(),
|
||||
'query' => $queryFirst
|
||||
]);
|
||||
|
||||
@ -33,4 +28,3 @@ class ActiveDataProviderCloningTest extends TestCase
|
||||
$this->assertNotSame($querySecond, $queryFirst);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
public function testActiveQuery()
|
||||
{
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => Order::find()->orderBy('id'),
|
||||
]);
|
||||
$orders = $provider->getModels();
|
||||
@ -46,7 +45,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
$this->assertEquals([1, 2, 3], $provider->getKeys());
|
||||
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => Order::find(),
|
||||
'pagination' => [
|
||||
'pageSize' => 2,
|
||||
@ -61,7 +59,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
/* @var $customer Customer */
|
||||
$customer = Customer::findOne(2);
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $customer->getOrders(),
|
||||
]);
|
||||
$orders = $provider->getModels();
|
||||
@ -71,7 +68,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
$this->assertEquals([2, 3], $provider->getKeys());
|
||||
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $customer->getOrders(),
|
||||
'pagination' => [
|
||||
'pageSize' => 1,
|
||||
@ -86,7 +82,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
/* @var $order Order */
|
||||
$order = Order::findOne(2);
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $order->getItems(),
|
||||
]);
|
||||
$items = $provider->getModels();
|
||||
@ -97,7 +92,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
$this->assertEquals([3, 4, 5], $provider->getKeys());
|
||||
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $order->getItems(),
|
||||
'pagination' => [
|
||||
'pageSize' => 2,
|
||||
@ -112,7 +106,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
/* @var $order Order */
|
||||
$order = Order::findOne(1);
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $order->getBooks(),
|
||||
]);
|
||||
$items = $provider->getModels();
|
||||
@ -121,7 +114,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
||||
$this->assertInstanceOf(Item::className(), $items[1]);
|
||||
|
||||
$provider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => $order->getBooks(),
|
||||
'pagination' => [
|
||||
'pageSize' => 1,
|
||||
|
@ -34,7 +34,6 @@ class LinkSorterTest extends DatabaseTestCase
|
||||
public function testLabelsSimple()
|
||||
{
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => Order::find(),
|
||||
'models' => [new Order()],
|
||||
'totalCount' => 1,
|
||||
@ -59,7 +58,6 @@ class LinkSorterTest extends DatabaseTestCase
|
||||
public function testLabelsExplicit()
|
||||
{
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'db' => $this->getConnection(),
|
||||
'query' => Order::find(),
|
||||
'models' => [new Order()],
|
||||
'totalCount' => 1,
|
||||
@ -88,7 +86,7 @@ class LinkSorterTest extends DatabaseTestCase
|
||||
public function testShouldTriggerInitEvent()
|
||||
{
|
||||
$initTriggered = false;
|
||||
$linkSorter = new LinkSorter(
|
||||
new LinkSorter(
|
||||
[
|
||||
'sort' => [
|
||||
'attributes' => ['total'],
|
||||
|
Reference in New Issue
Block a user