Laravel钩入雄辩前后保存为每个模特儿 [英] laravel hook into Eloquent pre and post save for every model

查看:100
本文介绍了Laravel钩入雄辩前后保存为每个模特儿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Laravel和ORM总体来说很新。在任何型号的保存之前和之后,我如何能够勾勒出雄辩的火焰代码?我知道我可以为特定的型号做下列事情,但我正在寻找如何为每一个模型做这个。

  class页面扩展Eloquent {

public function save()
{
//保存代码之前
parent :: save();
//保存代码
}
}


解决方案

您可以创建一个扩展雄辩的BaseModel类,然后让所有的模型都扩展为BaseModel。这里有一个例子:

 抽象类优雅延伸卓越{

/ *保存***** ********************* /
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 - >存在;
if($ validate)
if(!$ this-> valid())return false;
if($ newRecord)
$ this-> preNew();

$ before = is_null($ preSave)? $ this-> preSave():$ preSave($ this);
// check before&有效,然后传递给父
$ 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); //执行onForceSave
返回$之前? parent :: save():false; //保存,无论验证结果如何
}

/ **软删除*********************** ***** /
public function preSoftDelete(){return true;
public function postSoftDelete(){}
public function softDelete($ val = true,$ preSoftDelete = null,$ postSoftDelete = null)
{
if($ this-> ;存在)
{
$ 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;
}
}

/ **硬删除*************************** * /
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;
}
}
}


I'm new to Laravel and ORM's in general. How could i hook into Eloquent to fire code before and after a save of any model? I know i can do the following for specific models but i'm looking at figuring out how to do this for every model.

class Page extends Eloquent {

   public function save()
   {
      // before save code 
      parent::save();
      // after save code
   }
}

解决方案

You can create a BaseModel class that extends eloquent and then have all your models extend BaseModel. Here's an example:

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;
    }
}
}

这篇关于Laravel钩入雄辩前后保存为每个模特儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆