Commit df023545 by Klimov Paul

Fixed `yii\mongodb\ActiveRecord` saves `null` as `_id`, if attributes are empty

parent d0ee41e4
...@@ -222,7 +222,9 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -222,7 +222,9 @@ abstract class ActiveRecord extends BaseActiveRecord
if (empty($values)) { if (empty($values)) {
$currentAttributes = $this->getAttributes(); $currentAttributes = $this->getAttributes();
foreach ($this->primaryKey() as $key) { foreach ($this->primaryKey() as $key) {
$values[$key] = isset($currentAttributes[$key]) ? $currentAttributes[$key] : null; if (isset($currentAttributes[$key])) {
$values[$key] = $currentAttributes[$key];
}
} }
} }
$newId = static::getCollection()->insert($values); $newId = static::getCollection()->insert($values);
......
...@@ -4,6 +4,7 @@ Yii Framework 2 mongodb extension Change Log ...@@ -4,6 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.0.1 under development 2.0.1 under development
----------------------- -----------------------
- Bug #6026: Fixed `yii\mongodb\ActiveRecord` saves `null` as `_id`, if attributes are empty (klimov-paul)
- Enh #3855: Added debug toolbar panel for MongoDB (klimov-paul) - Enh #3855: Added debug toolbar panel for MongoDB (klimov-paul)
- Enh #5592: Added support for 'findAndModify' operation at `yii\mongodb\Query` and `yii\mongodb\ActiveQuery` (klimov-paul) - Enh #5592: Added support for 'findAndModify' operation at `yii\mongodb\Query` and `yii\mongodb\ActiveQuery` (klimov-paul)
......
...@@ -275,4 +275,18 @@ class ActiveRecordTest extends MongoDbTestCase ...@@ -275,4 +275,18 @@ class ActiveRecordTest extends MongoDbTestCase
$this->assertTrue($customer instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals($newName, $customer->name); $this->assertEquals($newName, $customer->name);
} }
/**
* @depends testInsert
*
* @see https://github.com/yiisoft/yii2/issues/6026
*/
public function testInsertEmptyAttributes()
{
$record = new Customer();
$record->save(false);
$this->assertTrue($record->_id instanceof \MongoId);
$this->assertFalse($record->isNewRecord);
}
} }
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