fixed bug when deleting eloquent models.

This commit is contained in:
Taylor Otwell 2011-06-13 21:34:53 -05:00
parent 05b2e28770
commit 285cdcc8f2
2 changed files with 41 additions and 2 deletions

View File

@ -178,12 +178,40 @@ abstract class Eloquent {
*/
public function save()
{
// -----------------------------------------------------
// If the model doesn't have any dirty attributes, there
// is no need to save it to the database.
// -----------------------------------------------------
if ($this->exists and count($this->dirty) == 0)
{
return true;
}
return Eloquent\Warehouse::store($this);
$result = Eloquent\Warehouse::put($this);
// -----------------------------------------------------
// The dirty attributes can be cleared after each save.
// -----------------------------------------------------
$this->dirty = array();
return $result;
}
/**
* Delete a model from the database.
*/
public function delete($id = null)
{
// -----------------------------------------------------
// If the method is being called from an existing model,
// only delete that model from the database.
// -----------------------------------------------------
if ($this->exists)
{
return Eloquent\Warehouse::forget($this);
}
return $this->query->delete($id);
}
/**

View File

@ -8,7 +8,7 @@ class Warehouse {
* @param object $eloquent
* @return bool
*/
public static function store($eloquent)
public static function put($eloquent)
{
$model = get_class($eloquent);
@ -39,6 +39,17 @@ class Warehouse {
return true;
}
/**
* Delete an Eloquent model from the database.
*
* @param object $eloquent
* @return bool
*/
public static function forget($eloquent)
{
return (\System\DB::table(Meta::table(get_class($eloquent)))->where('id', '=', $eloquent->id)->delete() == 1);
}
/**
* Set the activity timestamps on a model.
*