当从数据库检索到Eloquent模型时执行代码 [英] Execute code when Eloquent model is retrieved from database

查看:82
本文介绍了当从数据库检索到Eloquent模型时执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个雄辩的模特儿。无论何时从数据库检索,我想检查一个条件是否满足,并设置一个模型属性,如果是这样的情况。



编辑:我最初认为恢复事件将是放置相关逻辑的正确位置,但正如Tyler Crompton指出的,恢复被触发在软删除记录恢复之前。

解决方案

您有两个有效的选项: p>



如果我是你,我会和第一个选项一起去,这是我如何做的:

 <?php namespace \Illuminate\Database\Eloquent; 

抽象类LoadModel extends Model {

/ **
*使用调度程序注册一个加载的模型事件。
*
* @param \Closure | string $ callback
* @return void
* /
public static function loaded($ callback)
{
static :: registerModelEvent('loaded',$ callback);
}

/ **
*获取可观察的事件名称。
*
* @return array
* /
public function getObservableEvents()
{
return array_merge(parent :: getObservableEvents(),array('加载'));
}

/ **
*创建一个已存在的新模型实例。
*
* @param array $ attributes
* @return \Illuminate\Database\Eloquent\Model | static
* /
public function newFromBuilder $ attributes = array())
{
$ instance = parent :: newFromBuilder($ attributes);

$ instance-> fireModelEvent('loaded',false);

return $ instance;
}

}

只需确保有问题的模型子类来自 LoadingModule 。我已经确认这个工作,因为我发现一个很好的用例。较早版本的PHP将MySQL值作为字符串返回。通常,PHP将在数字操作中将其静默地转换为各自的数字类型。但是,转换为JSON不被视为数字操作。 JSON值表示为字符串。这可能会导致我的API的客户端出现问题。所以我向我的模型添加了一个加载的事件,将值转换为正确的类型。


I have an Eloquent model. Whenever it is retrieved from the database I would like to check whether a condition is fulfilled and set a model attribute if this is the case.

EDIT: I initially thought that the restoring event would be the right place to put the relevant logic, but as Tyler Crompton points out below, restoring is fired before a soft-deleted record is restored.

解决方案

You have two valid options:

If I were you, I'd go with the first option and this is how I'd do it:

<?php namespace \Illuminate\Database\Eloquent;

abstract class LoadingModel extends Model {

    /**
     * Register a loaded model event with the dispatcher.
     *
     * @param  \Closure|string  $callback
     * @return void
     */
    public static function loaded($callback)
    {
        static::registerModelEvent('loaded', $callback);
    }

    /**
     * Get the observable event names.
     *
     * @return array
     */
    public function getObservableEvents()
    {
        return array_merge(parent::getObservableEvents(), array('loaded'));
    }

    /**
     * Create a new model instance that is existing.
     *
     * @param  array  $attributes
     * @return \Illuminate\Database\Eloquent\Model|static
     */
    public function newFromBuilder($attributes = array())
    {
        $instance = parent::newFromBuilder($attributes);

        $instance->fireModelEvent('loaded', false);

        return $instance;
    }

}

Just make sure the models in question subclass from LoadingModule. I have confirmed this to work as I found a great use case for it. Older versions of PHP returned MySQL values as strings. Normally, PHP will silently cast these to their respective numeric types in numeric operations. However, converting to JSON is not considered a numeric operation. The JSON values are represented as strings. This can cause problems for clients of my API. So I added a loaded event to my models to convert values to the correct type.

这篇关于当从数据库检索到Eloquent模型时执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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