some fixes

This commit is contained in:
Parziphal 2013-10-12 05:37:13 -05:00
parent 144c021616
commit f26182f567
3 changed files with 40 additions and 30 deletions

View File

@ -76,15 +76,23 @@ abstract class Base
} }
/**
* Finds model by id and updates it.
*
* @param string|array $id
* @param array $attrs
*/
static public function update($id, array $attrs) static public function update($id, array $attrs)
{ {
$attrs_str = []; if (is_array($id)) {
foreach (array_keys($attrs) as $attr) foreach ($id as $k => $i) {
$attrs_str[] = '`'.$attr.'` = ?'; static::update($i, $attrs[$k]);
$sql = "UPDATE `" . static::tableName() . "` SET " . implode(', ', $attrs_str) . " WHERE id = ?"; }
array_unshift($attrs, $sql); } else {
$attrs[] = $id; $object = static::find($id);
static::connection()->executeSql($attrs); $object->updateAttributes($attrs);
return $object;
}
} }
/** /**
@ -369,24 +377,6 @@ abstract class Base
return $this->isNewRecord; return $this->isNewRecord;
} }
public function updateAttributes(array $attrs)
{
$this->assignAttributes($attrs);
$this->runCallbacks('before_update');
/**
* iTODO: Must let know save() we're updating, so it will
* validate data with action "update" and not "save".
* Should separate save() and make this and update_attribute call
* something like update()?
*/
if ($this->save(['action' => 'update'])) {
$this->runCallbacks('after_update');
return true;
}
return false;
}
/** /**
* Directly passes the new value to the attributes array and then * Directly passes the new value to the attributes array and then
* saves the record. * saves the record.
@ -610,7 +600,7 @@ abstract class Base
return $current; return $current;
} }
protected function runCallbacks($callback_name) public function runCallbacks($callback_name)
{ {
$callbacks = array(); $callbacks = array();

View File

@ -103,7 +103,9 @@ trait AttributeMethods
); );
} }
if ((string)$this->getAttribute($name) != (string)$value) { if ($this->isNewRecord) {
$this->setChangedAttribute($name, $value);
} elseif ((string)$this->getAttribute($name) != (string)$value) {
$this->setChangedAttribute($name, $this->$name); $this->setChangedAttribute($name, $this->$name);
} }
$this->attributes[$name] = $value; $this->attributes[$name] = $value;
@ -121,6 +123,11 @@ trait AttributeMethods
return isset($this->attributes[$name]); return isset($this->attributes[$name]);
} }
public function attributes()
{
return $this->attributes;
}
/** /**
* Add/change attributes to model * Add/change attributes to model
* *
@ -176,9 +183,22 @@ trait AttributeMethods
} }
} }
public function attributes() public function updateAttributes(array $attrs)
{ {
return $this->attributes; $this->assignAttributes($attrs);
$this->runCallbacks('before_update');
/**
* iTODO: Must let know save() we're updating, so it will
* validate data with action "update" and not "save".
* Should separate save() and make this and update_attribute call
* something like update()?
*/
if ($this->save(['action' => 'update'])) {
$this->runCallbacks('after_update');
return true;
}
return false;
} }
/** /**

View File

@ -40,7 +40,7 @@ class Association extends AbstractRelation
# The function is binded to the relation object. # The function is binded to the relation object.
if (isset($this->params[0])) { if (isset($this->params[0])) {
$lambda = array_shift($this->params); $lambda = array_shift($this->params);
$lambda->bindTo($this); $lambda = $lambda->bindTo($query);
$lambda(); $lambda();
} }