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
|
class ActiveDataProvider extends BaseDataProvider
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var QueryInterface the query that is used to fetch data models and [[totalCount]]
|
* @var QueryInterface|null the query that is used to fetch data models and [[totalCount]] if it is not explicitly set.
|
||||||
* if it is not explicitly set.
|
|
||||||
*/
|
*/
|
||||||
public $query;
|
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.
|
* 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:
|
* 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;
|
public $key;
|
||||||
/**
|
/**
|
||||||
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
|
* @var Connection|array|string|null the DB connection object or the application component ID of the DB connection.
|
||||||
* If not set, the default DB connection will be used.
|
* 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.
|
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
|
||||||
*/
|
*/
|
||||||
public $db;
|
public $db;
|
||||||
@ -82,13 +81,15 @@ class ActiveDataProvider extends BaseDataProvider
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the DB connection component.
|
* 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.
|
* @throws InvalidConfigException if [[db]] is invalid.
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::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)
|
public function setSort($value)
|
||||||
{
|
{
|
||||||
parent::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 */
|
/* @var $modelClass Model */
|
||||||
$modelClass = $this->query->modelClass;
|
$modelClass = $this->query->modelClass;
|
||||||
$model = $modelClass::instance();
|
$model = $modelClass::instance();
|
||||||
|
@ -8,21 +8,16 @@
|
|||||||
namespace yiiunit\framework\data;
|
namespace yiiunit\framework\data;
|
||||||
|
|
||||||
use yii\data\ActiveDataProvider;
|
use yii\data\ActiveDataProvider;
|
||||||
use yii\db\Connection;
|
|
||||||
use yii\db\Query;
|
use yii\db\Query;
|
||||||
use yiiunit\TestCase;
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
class ActiveDataProviderCloningTest extends TestCase
|
class ActiveDataProviderCloningTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
// Tests :
|
|
||||||
|
|
||||||
public function testClone()
|
public function testClone()
|
||||||
{
|
{
|
||||||
$queryFirst = new Query();
|
$queryFirst = new Query();
|
||||||
|
|
||||||
$dataProviderFirst = new ActiveDataProvider([
|
$dataProviderFirst = new ActiveDataProvider([
|
||||||
'db' => new Connection(),
|
|
||||||
'query' => $queryFirst
|
'query' => $queryFirst
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -33,4 +28,3 @@ class ActiveDataProviderCloningTest extends TestCase
|
|||||||
$this->assertNotSame($querySecond, $queryFirst);
|
$this->assertNotSame($querySecond, $queryFirst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
public function testActiveQuery()
|
public function testActiveQuery()
|
||||||
{
|
{
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => Order::find()->orderBy('id'),
|
'query' => Order::find()->orderBy('id'),
|
||||||
]);
|
]);
|
||||||
$orders = $provider->getModels();
|
$orders = $provider->getModels();
|
||||||
@ -46,7 +45,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
$this->assertEquals([1, 2, 3], $provider->getKeys());
|
$this->assertEquals([1, 2, 3], $provider->getKeys());
|
||||||
|
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => Order::find(),
|
'query' => Order::find(),
|
||||||
'pagination' => [
|
'pagination' => [
|
||||||
'pageSize' => 2,
|
'pageSize' => 2,
|
||||||
@ -61,7 +59,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
/* @var $customer Customer */
|
/* @var $customer Customer */
|
||||||
$customer = Customer::findOne(2);
|
$customer = Customer::findOne(2);
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $customer->getOrders(),
|
'query' => $customer->getOrders(),
|
||||||
]);
|
]);
|
||||||
$orders = $provider->getModels();
|
$orders = $provider->getModels();
|
||||||
@ -71,7 +68,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
$this->assertEquals([2, 3], $provider->getKeys());
|
$this->assertEquals([2, 3], $provider->getKeys());
|
||||||
|
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $customer->getOrders(),
|
'query' => $customer->getOrders(),
|
||||||
'pagination' => [
|
'pagination' => [
|
||||||
'pageSize' => 1,
|
'pageSize' => 1,
|
||||||
@ -86,7 +82,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
/* @var $order Order */
|
/* @var $order Order */
|
||||||
$order = Order::findOne(2);
|
$order = Order::findOne(2);
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $order->getItems(),
|
'query' => $order->getItems(),
|
||||||
]);
|
]);
|
||||||
$items = $provider->getModels();
|
$items = $provider->getModels();
|
||||||
@ -97,7 +92,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
$this->assertEquals([3, 4, 5], $provider->getKeys());
|
$this->assertEquals([3, 4, 5], $provider->getKeys());
|
||||||
|
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $order->getItems(),
|
'query' => $order->getItems(),
|
||||||
'pagination' => [
|
'pagination' => [
|
||||||
'pageSize' => 2,
|
'pageSize' => 2,
|
||||||
@ -112,7 +106,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
/* @var $order Order */
|
/* @var $order Order */
|
||||||
$order = Order::findOne(1);
|
$order = Order::findOne(1);
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $order->getBooks(),
|
'query' => $order->getBooks(),
|
||||||
]);
|
]);
|
||||||
$items = $provider->getModels();
|
$items = $provider->getModels();
|
||||||
@ -121,7 +114,6 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
|
|||||||
$this->assertInstanceOf(Item::className(), $items[1]);
|
$this->assertInstanceOf(Item::className(), $items[1]);
|
||||||
|
|
||||||
$provider = new ActiveDataProvider([
|
$provider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => $order->getBooks(),
|
'query' => $order->getBooks(),
|
||||||
'pagination' => [
|
'pagination' => [
|
||||||
'pageSize' => 1,
|
'pageSize' => 1,
|
||||||
|
@ -34,7 +34,6 @@ class LinkSorterTest extends DatabaseTestCase
|
|||||||
public function testLabelsSimple()
|
public function testLabelsSimple()
|
||||||
{
|
{
|
||||||
$dataProvider = new ActiveDataProvider([
|
$dataProvider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => Order::find(),
|
'query' => Order::find(),
|
||||||
'models' => [new Order()],
|
'models' => [new Order()],
|
||||||
'totalCount' => 1,
|
'totalCount' => 1,
|
||||||
@ -59,7 +58,6 @@ class LinkSorterTest extends DatabaseTestCase
|
|||||||
public function testLabelsExplicit()
|
public function testLabelsExplicit()
|
||||||
{
|
{
|
||||||
$dataProvider = new ActiveDataProvider([
|
$dataProvider = new ActiveDataProvider([
|
||||||
'db' => $this->getConnection(),
|
|
||||||
'query' => Order::find(),
|
'query' => Order::find(),
|
||||||
'models' => [new Order()],
|
'models' => [new Order()],
|
||||||
'totalCount' => 1,
|
'totalCount' => 1,
|
||||||
@ -88,7 +86,7 @@ class LinkSorterTest extends DatabaseTestCase
|
|||||||
public function testShouldTriggerInitEvent()
|
public function testShouldTriggerInitEvent()
|
||||||
{
|
{
|
||||||
$initTriggered = false;
|
$initTriggered = false;
|
||||||
$linkSorter = new LinkSorter(
|
new LinkSorter(
|
||||||
[
|
[
|
||||||
'sort' => [
|
'sort' => [
|
||||||
'attributes' => ['total'],
|
'attributes' => ['total'],
|
||||||
|
Reference in New Issue
Block a user