2013-02-15 6 views
5

Ich bin neu bei Laravel und ORM im Allgemeinen. Wie könnte ich mich in Eloquent einklinken, um Code vor und nach dem Speichern eines Modells zu löschen? Ich weiß, dass ich für bestimmte Modelle Folgendes tun kann, aber ich überlege mir, wie man das für jedes Modell macht.Laravel Haken in Eloquent Pre und Post speichern für jedes Modell

+0

sehen Sie, dass ein Haken speichern möchten, die von eloquent erweitert für alle Modelle ist vorhanden? – Bilal

Antwort

3

Sie können eine BaseModel-Klasse erstellen, die eloquent erweitert und dann alle Modelle BaseModel erweitern lassen. Hier ist ein Beispiel:

abstract class Elegant extends Eloquent{ 

/* Save ****************************/ 
public function preNew() {} 
public function postNew() {} 
public function preSave() { return true; } 
public function postSave() {} 
public function save($validate=true, $preSave=null, $postSave=null) 
{ 
    $newRecord = !$this->exists; 
    if ($validate) 
     if (!$this->valid()) return false; 
    if($newRecord) 
     $this->preNew(); 

    $before = is_null($preSave) ? $this->preSave() : $preSave($this); 
     // check before & valid, then pass to parent 
    $success = ($before) ? parent::save() : false; 
    if ($success) 
     is_null($postSave) ? $this->postSave() : $postSave($this); 
    if($newRecord) 
     $this->postNew(); 
    return $success; 
} 
public function onForceSave(){} 
public function forceSave($validate=true, $rules=array(), $messages=array(), $onForceSave=null) 
{ 
    if ($validate) 
     $this->valid($rules, $messages); 
    $before = is_null($onForceSave) ? $this->onForceSave() : $onForceSave($this); // execute onForceSave 
    return $before ? parent::save() : false; // save regardless of the result of validation 
} 

/** Soft Delete ****************************/ 
public function preSoftDelete() { return true; } 
public function postSoftDelete() { } 
public function softDelete($val = true, $preSoftDelete=null, $postSoftDelete=null) 
{ 
    if ($this->exists) 
    { 
     $before = is_null($preSoftDelete) ? $this->preSoftDelete() : $preSoftDelete($this); 
     $success = null; 
     if($before) { 
      $this->set_attribute(static::$softDelete, $val); 
      $success = $this->save(false); 
     } 
     else 
      $success = false; 
     if ($success) 
     { 
      is_null($postSoftDelete) ? $this->postSoftDelete() : $postSoftDelete($this); 
     } 
     return $success; 
    } 
} 

/** Hard Delete ****************************/ 
public function preDelete() { return true;} 
public function postDelete(){} 
public function delete($preDelete=null, $postDelete=null) 
{ 
    if ($this->exists) 
    { 
     $before = is_null($preDelete) ? $this->preDelete() : $preDelete($this); 
     $success = ($before) ? parent::delete() : false; 
     if ($success) 
     { 
      is_null($postDelete) ? $this->postDelete() : $postDelete($this); 
     } 
     return $success; 
    } 
} 
} 
+0

das ist eine gute Idee. Vielen Dank – David

11

Es gibt sogar einen besseren Weg, dies zu erreichen! Erstellen Sie einen Beobachter für, lässt ein Modell House genannt sagen:

class HouseObserver { 

    public function saving(House $house) { 
     // Code before save 
    } 

    public function saved(House $house) { 
     // Code after save 
    } 

} 

nun den Betrachter mit dem House Modell registrieren, indem Sie die Zeile hinzufügen House::observe(new HouseObserver) somehwere. Die Linie kann in der Startmethode des Modells hinzugefügt werden:

class House extends Eloquent { 

    // Lots of model code 

    public static function boot() { 
     parent::boot(); 
     self::observe(new HouseObserver); 
    } 

} 

Weitere Daten können here finden.

1

Mit Laravel Modelle besitzen Lebenszyklusereignisse dieses

leicht lösen kann
/** 
* model life cycle event listeners 
*/ 
public static function boot(){ 
    parent::boot(); 

    static::creating(function ($instance){ 
     // 
    }); 

    static::created(function ($instance){ 
     // 
    }); 
}