Commit 4e8e22da by Carsten Brandt

fixed issue with elasticsearch >=1.2.0

do not use scripting as it is disabled by default and will cause edge case queries to fail fixes #4187
parent 489a5e36
...@@ -205,8 +205,8 @@ class QueryBuilder extends \yii\base\Object ...@@ -205,8 +205,8 @@ class QueryBuilder extends \yii\base\Object
$parts = []; $parts = [];
foreach ($condition as $attribute => $value) { foreach ($condition as $attribute => $value) {
if ($attribute == '_id') { if ($attribute == '_id') {
if ($value == null) { // there is no null pk if ($value === null) { // there is no null pk
$parts[] = ['script' => ['script' => '0==1']]; $parts[] = ['terms' => ['_uid' => []]]; // this condition is equal to WHERE false
} else { } else {
$parts[] = ['ids' => ['values' => is_array($value) ? $value : [$value]]]; $parts[] = ['ids' => ['values' => is_array($value) ? $value : [$value]]];
} }
...@@ -287,7 +287,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -287,7 +287,7 @@ class QueryBuilder extends \yii\base\Object
$values = (array) $values; $values = (array) $values;
if (empty($values) || $column === []) { if (empty($values) || $column === []) {
return $operator === 'in' ? ['script' => ['script' => '0==1']] : []; return $operator === 'in' ? ['terms' => ['_uid' => []]] : []; // this condition is equal to WHERE false
} }
if (count($column) > 1) { if (count($column) > 1) {
...@@ -307,7 +307,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -307,7 +307,7 @@ class QueryBuilder extends \yii\base\Object
} }
if ($column == '_id') { if ($column == '_id') {
if (empty($values) && $canBeNull) { // there is no null pk if (empty($values) && $canBeNull) { // there is no null pk
$filter = ['script' => ['script' => '0==1']]; $filter = ['terms' => ['_uid' => []]]; // this condition is equal to WHERE false
} else { } else {
$filter = ['ids' => ['values' => array_values($values)]]; $filter = ['ids' => ['values' => array_values($values)]];
if ($canBeNull) { if ($canBeNull) {
......
...@@ -667,5 +667,35 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -667,5 +667,35 @@ class ActiveRecordTest extends ElasticSearchTestCase
Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND); Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND);
} }
public function testFindEmptyPkCondition()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $orderItemClass \yii\db\ActiveRecordInterface */
$orderItemClass = $this->getOrderItemClass();
$orderItem = new $orderItemClass();
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false);
$orderItem->save(false);
$this->afterSave();
$orderItems = $orderItemClass::find()->where(['_id' => [$orderItem->getPrimaryKey()]])->all();
$this->assertEquals(1, count($orderItems));
$orderItems = $orderItemClass::find()->where(['_id' => []])->all();
$this->assertEquals(0, count($orderItems));
$orderItems = $orderItemClass::find()->where(['_id' => null])->all();
$this->assertEquals(0, count($orderItems));
$orderItems = $orderItemClass::find()->where(['IN', '_id', [$orderItem->getPrimaryKey()]])->all();
$this->assertEquals(1, count($orderItems));
$orderItems = $orderItemClass::find()->where(['IN', '_id', []])->all();
$this->assertEquals(0, count($orderItems));
$orderItems = $orderItemClass::find()->where(['IN', '_id', [null]])->all();
$this->assertEquals(0, count($orderItems));
}
// TODO test AR with not mapped PK // TODO test AR with not mapped PK
} }
...@@ -1046,4 +1046,22 @@ trait ActiveRecordTestTrait ...@@ -1046,4 +1046,22 @@ trait ActiveRecordTestTrait
Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND); Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND);
} }
public function testFindEmptyInCondition()
{
/* @var $customerClass \yii\db\ActiveRecordInterface */
$customerClass = $this->getCustomerClass();
/* @var $this TestCase|ActiveRecordTestTrait */
$customers = $customerClass::find()->where(['id' => [1]])->all();
$this->assertEquals(1, count($customers));
$customers = $customerClass::find()->where(['id' => []])->all();
$this->assertEquals(0, count($customers));
$customers = $customerClass::find()->where(['IN', 'id', [1]])->all();
$this->assertEquals(1, count($customers));
$customers = $customerClass::find()->where(['IN', 'id', []])->all();
$this->assertEquals(0, count($customers));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment