Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the "unset field" feature #323

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
*/
abstract class ActiveRecord extends BaseActiveRecord
{

protected $unsetAttrs = [];

/**
* Returns the Mongo connection used by this AR class.
* By default, the "mongodb" application component is used as the Mongo connection.
Expand Down Expand Up @@ -165,6 +168,28 @@ public function attributes()
throw new InvalidConfigException('The attributes() method of mongodb ActiveRecord has to be implemented by child classes.');
}

public function __set($name, $value)
{
unset($this->unsetAttrs[$name]);
parent::__set($name,$value);
}

public function unset()
{
foreach(func_get_args() as $attribute) {
if (!$this->hasAttribute($attribute)) {
throw new UnknownPropertyException('Unsetting unknown property: ' . get_class($this) . '::' . $attribute);
}
if ($this->getIsNewRecord()) {
unset($this->$attribute);
}
else {
$this->$attribute = null;
$this->unsetAttrs[$attribute] = '';
}
}
}

/**
* Inserts a row into the associated Mongo collection using the attribute values of this record.
*
Expand Down Expand Up @@ -253,7 +278,7 @@ protected function updateInternal($attributes = null)
return false;
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
if (empty($values) && empty($this->unsetAttrs)) {
$this->afterSave(false, $values);
return 0;
}
Expand All @@ -265,9 +290,25 @@ protected function updateInternal($attributes = null)
}
$condition[$lock] = $this->$lock;
}

$document = $values;
if (!empty($this->unsetAttrs)){
foreach ($this->unsetAttrs as $attr => $_) {
unset($document[$attr],$values[$attr],$this->$attr);
}
if (empty($document)) {
$document = ['$unset' => $this->unsetAttrs];
} else {
$document = [
'$set' => $document,
'$unset' => $this->unsetAttrs,
];
}
$this->unsetAttrs = [];
}
// We do not check the return value of update() because it's possible
// that it doesn't change anything and thus returns 0.
$rows = static::getCollection()->update($condition, $values);
$rows = static::getCollection()->update($condition, $document);

if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.');
Expand Down