diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 3ae8485db1..1470e9e857 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -10,6 +10,7 @@ namespace yii\elasticsearch; use Yii; use yii\base\InvalidCallException; use yii\base\InvalidConfigException; +use yii\base\NotSupportedException; use yii\db\BaseActiveRecord; use yii\helpers\ArrayHelper; use yii\helpers\Inflector; @@ -604,4 +605,14 @@ class ActiveRecord extends BaseActiveRecord return $n; } + + /** + * Destroys the relationship in current model. + * + * This method is not supported by elasticsearch. + */ + public function unlinkAll($name, $delete = false) + { + throw new NotSupportedException('unlinkAll() is not supported by elasticsearch, use unlink() instead.'); + } } diff --git a/extensions/elasticsearch/CHANGELOG.md b/extensions/elasticsearch/CHANGELOG.md index 0d96dac2ae..70085163ca 100644 --- a/extensions/elasticsearch/CHANGELOG.md +++ b/extensions/elasticsearch/CHANGELOG.md @@ -6,7 +6,6 @@ Yii Framework 2 elasticsearch extension Change Log - Bug #3587: Fixed an issue with storing empty records (cebe) - Bug #4187: Elasticsearch dynamic scripting is disabled in 1.2.0, so do not use it in query builder (cebe) -- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) - Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales) - Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) diff --git a/tests/unit/extensions/elasticsearch/ActiveRecordTest.php b/tests/unit/extensions/elasticsearch/ActiveRecordTest.php index 255a19d504..385d43ebb3 100644 --- a/tests/unit/extensions/elasticsearch/ActiveRecordTest.php +++ b/tests/unit/extensions/elasticsearch/ActiveRecordTest.php @@ -804,6 +804,9 @@ class ActiveRecordTest extends ElasticSearchTestCase $this->assertFalse(isset($items[2])); } + /** + * @expectedException \yii\base\NotSupportedException + */ public function testArrayAttributeRelationUnLinkAll() { /* @var $order Order */ @@ -825,14 +828,51 @@ class ActiveRecordTest extends ElasticSearchTestCase $this->assertEquals(0, count($items)); } - public function testUnlinkAllAndConditionSetNull() + public function testUnlinkAll() { - $this->markTestSkipped('https://github.com/yiisoft/yii2/issues/5136'); + // not supported by elasticsearch } + /** + * @expectedException \yii\base\NotSupportedException + */ + public function testUnlinkAllAndConditionSetNull() + { + /* @var $customerClass \yii\db\BaseActiveRecord */ + $customerClass = $this->getCustomerClass(); + /* @var $orderClass \yii\db\BaseActiveRecord */ + $orderClass = $this->getOrderWithNullFKClass(); + + // in this test all orders are owned by customer 1 + $orderClass::updateAll(['customer_id' => 1]); + $this->afterSave(); + + $customer = $customerClass::findOne(1); + $this->assertEquals(3, count($customer->ordersWithNullFK)); + $this->assertEquals(1, count($customer->expensiveOrdersWithNullFK)); + $this->assertEquals(3, $orderClass::find()->count()); + $customer->unlinkAll('expensiveOrdersWithNullFK'); + } + + /** + * @expectedException \yii\base\NotSupportedException + */ public function testUnlinkAllAndConditionDelete() { - $this->markTestSkipped('https://github.com/yiisoft/yii2/issues/5136'); + /* @var $customerClass \yii\db\BaseActiveRecord */ + $customerClass = $this->getCustomerClass(); + /* @var $orderClass \yii\db\BaseActiveRecord */ + $orderClass = $this->getOrderWithNullFKClass(); + + // in this test all orders are owned by customer 1 + $orderClass::updateAll(['customer_id' => 1]); + $this->afterSave(); + + $customer = $customerClass::findOne(1); + $this->assertEquals(3, count($customer->ordersWithNullFK)); + $this->assertEquals(1, count($customer->expensiveOrdersWithNullFK)); + $this->assertEquals(3, $orderClass::find()->count()); + $customer->unlinkAll('expensiveOrdersWithNullFK', true); } // TODO test AR with not mapped PK