Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 4雄辩模型观察者事件的顺序_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel 4雄辩模型观察者事件的顺序

Php Laravel 4雄辩模型观察者事件的顺序,php,laravel,eloquent,Php,Laravel,Eloquent,根据,创建新项时将触发以下事件。创建,然后创建,然后保存,然后保存 然而,当我调用一个模型类时,事件是以相反的方式触发的。在创建之前调用保存 我的代码: class TBone extends Eloquent { protected $table = 'bone'; protected $primaryKey = 'id'; protected $guarded = array('id'); } class ObserverLeBone{ publi

根据,创建新项时将触发以下事件。创建,然后创建,然后保存,然后保存

然而,当我调用一个模型类时,事件是以相反的方式触发的。在创建之前调用保存

我的代码:

class TBone extends Eloquent {

    protected $table = 'bone';

    protected $primaryKey = 'id';

    protected $guarded = array('id');

}
class ObserverLeBone{

    public function creating($bone)
    {
        echo "creating\r\n";
    }

    public function saving($bone) 
    {
        echo "saving\r\n";
    }

    public function updating($bone) 
    {
        echo "updating\r\n";
    }
}
class EloquentTest extends TestCase {

    public function testObserver()
    {
       TBone::observe(new ObserverLeBone());
       $attributes = array('appreciatedAs' => 'Steak'); 
       TBone::create($attributes);
    }

}
saving
creating
观察者类:

class TBone extends Eloquent {

    protected $table = 'bone';

    protected $primaryKey = 'id';

    protected $guarded = array('id');

}
class ObserverLeBone{

    public function creating($bone)
    {
        echo "creating\r\n";
    }

    public function saving($bone) 
    {
        echo "saving\r\n";
    }

    public function updating($bone) 
    {
        echo "updating\r\n";
    }
}
class EloquentTest extends TestCase {

    public function testObserver()
    {
       TBone::observe(new ObserverLeBone());
       $attributes = array('appreciatedAs' => 'Steak'); 
       TBone::create($attributes);
    }

}
saving
creating
测试:

class TBone extends Eloquent {

    protected $table = 'bone';

    protected $primaryKey = 'id';

    protected $guarded = array('id');

}
class ObserverLeBone{

    public function creating($bone)
    {
        echo "creating\r\n";
    }

    public function saving($bone) 
    {
        echo "saving\r\n";
    }

    public function updating($bone) 
    {
        echo "updating\r\n";
    }
}
class EloquentTest extends TestCase {

    public function testObserver()
    {
       TBone::observe(new ObserverLeBone());
       $attributes = array('appreciatedAs' => 'Steak'); 
       TBone::create($attributes);
    }

}
saving
creating
运行测试用例时的输出:

class TBone extends Eloquent {

    protected $table = 'bone';

    protected $primaryKey = 'id';

    protected $guarded = array('id');

}
class ObserverLeBone{

    public function creating($bone)
    {
        echo "creating\r\n";
    }

    public function saving($bone) 
    {
        echo "saving\r\n";
    }

    public function updating($bone) 
    {
        echo "updating\r\n";
    }
}
class EloquentTest extends TestCase {

    public function testObserver()
    {
       TBone::observe(new ObserverLeBone());
       $attributes = array('appreciatedAs' => 'Steak'); 
       TBone::create($attributes);
    }

}
saving
creating
所以我只是想知道为什么在创建事件之前触发saving事件?
还是我遗漏了什么?

不确定这是错误还是功能,但根据代码,create调用save,您是对的:

public static function create(array $attributes)
{
    $model = new static($attributes);

    $model->save();

    return $model;
}
然后保存触发事件:

public function save(array $options = array())
{
    $query = $this->newQueryWithDeleted();

    // If the "saving" event returns false we'll bail out of the save and return
    // false, indicating that the save failed. This gives an opportunities to
    // listeners to cancel save operations if validations fail or whatever.
    if ($this->fireModelEvent('saving') === false)
    {
        return false;
    }

            ....
执行之前,插入(创建):

触发创建事件

if ($this->fireModelEvent('creating') === false) return false;

是的,这更像是一个术语集群。我猜想。创建意味着在内存中创建一些东西。保存意味着在数据库中插入一些内容。而laravel似乎使用术语create作为“插入”。但是“更新”毫无意义。现在,我必须将逻辑放入保存事件处理程序中,以确定对象是否已被持久化,我认为这是不必要的,因为我有一个正在创建的事件。。。所以是的,没有虫子只是一种解释。。。