当从数据库检索到Eloquent模型时执行代码 [英] Execute code when Eloquent model is retrieved from database
问题描述
编辑:我最初认为恢复
事件将是放置相关逻辑的正确位置,但正如Tyler Crompton指出的,恢复
被触发在软删除记录恢复之前。
您有两个有效的选项: p>
- 您可以将
\Illuminate\Datebase\Eloquent\Model
来添加这样的事件。 li>
- 您可以修改
\Illuminate\Datebase\Eloquent\Model
添加(并且可能发送(非请求)拉请求到 GitHub上的Laravel )。根据问题1685 ,它看起来好像不想要。
如果我是你,我会和第一个选项一起去,这是我如何做的:
<?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:
- You can subclass
\Illuminate\Datebase\Eloquent\Model
to add such an event. - You can modify your copy of
\Illuminate\Datebase\Eloquent\Model
to add this (and possibly send an (unsolicited) pull request to Laravel on GitHub). According to Issue 1685, it looks as though they do not want it.
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屋!