Commit 38d05708 by Carsten Brandt

fixed unlink() for array valued attributes

return value would not be a valid array when json encoded after unlink(). fixes #6065
parent e5b92724
...@@ -5,6 +5,7 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -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
......
...@@ -1295,7 +1295,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -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;
......
...@@ -813,6 +813,31 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -813,6 +813,31 @@ class ActiveRecordTest extends ElasticSearchTestCase
} }
/** /**
* 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
*/ */
public function testArrayAttributeRelationUnLinkAll() public function testArrayAttributeRelationUnLinkAll()
......
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