fixed unlink() for array valued attributes

return value would not be a valid array when json encoded after unlink().

fixes #6065
This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
Carsten Brandt
2014-11-15 21:42:36 +01:00
gitea-unlock(16/)
parent e5b927244c
commit 38d0570843
octicon-diff(16/tw-mr-1) 3 changed files with 27 additions and 1 deletions

1
extensions/elasticsearch/CHANGELOG.md
View File

@@ -5,6 +5,7 @@ Yii Framework 2 elasticsearch extension Change Log
----------------------- -----------------------
- Bug #5662: Elasticsearch AR updateCounters() now uses explicitly `groovy` script for updating making it compatible with ES >1.3.0 (cebe) - Bug #5662: Elasticsearch AR updateCounters() now uses explicitly `groovy` script for updating making it compatible with ES >1.3.0 (cebe)
- Bug #6065: `ActiveRecord::unlink()` was failing in some situations when working with relations via array valued attributes (cebe)
2.0.0 October 12, 2014 2.0.0 October 12, 2014

2
framework/db/BaseActiveRecord.php
View File

@@ -1295,7 +1295,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
if (($key = array_search($model->$a, $this->$b, false)) !== false) { if (($key = array_search($model->$a, $this->$b, false)) !== false) {
$values = $this->$b; $values = $this->$b;
unset($values[$key]); unset($values[$key]);
$this->$b = $values; $this->$b = array_values($values);
} }
} else { } else {
$this->$b = null; $this->$b = null;

25
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
View File

@@ -812,6 +812,31 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this->assertFalse(isset($items[2])); $this->assertFalse(isset($items[2]));
} }
/**
* https://github.com/yiisoft/yii2/issues/6065
*/
public function testArrayAttributeRelationUnLinkBrokenArray()
{
/* @var $order Order */
$order = Order::find()->where(['id' => 1])->one();
$itemIds = $order->itemsArray;
$removeId = reset($itemIds);
$item = Item::get($removeId);
$order->unlink('itemsByArrayValue', $item);
$this->afterSave();
$items = $order->itemsByArrayValue;
$this->assertEquals(1, count($items));
$this->assertFalse(isset($items[$removeId]));
// check also after refresh
$this->assertTrue($order->refresh());
$items = $order->itemsByArrayValue;
$this->assertEquals(1, count($items));
$this->assertFalse(isset($items[$removeId]));
}
/** /**
* @expectedException \yii\base\NotSupportedException * @expectedException \yii\base\NotSupportedException
*/ */