首页 \ 问答 \ 当表中没有实际变化时,Laravel Eloquent会监听事件(Laravel Eloquent listen events when there aren't actually changes in table)

当表中没有实际变化时,Laravel Eloquent会监听事件(Laravel Eloquent listen events when there aren't actually changes in table)

我需要在模型中监听更新事件,但我需要对表中不存在的一些变量进行建模。 我是通过将公共变量添加到模型中来实现的,但是当我仅对这些“假”属性进行更改时,我无法听取。 反正有没有将'假'属性传递给模型并且可以监听更新?

这是我的示例课程。

<?php

class School extends Eloquent {
    protected $table = 'schools';

    protected $fillable = array('name', 'type_id', 'city');
    protected $guarded = array('id');

    public $set_specialties;

    protected static function boot()
    {
        parent::boot();

        static::updating(function($model)
        {
            $data = array(
                'name' => $model->name,
                'type_id' => $model->type_id,
                'specialties' => $model->set_specialties,
                'city_id' => $model->city_id
            );

            $rules = array(
                'name' => 'required|min:3|max:50',
                'type_id' => 'required|min:1|max:300000',
                'specialties' => 'required|array',
                'city_id' => 'required|min:1|max:300000'
            );

            $validator = Validator::make($data, $rules);

            if ($validator->fails()) {
                throw new ValidationException(null, null, null, $validator->messages());
            } else {    
                return true;
            }
        });

        static::updated(function($model)
        {
            if ( $model->set_specialties != null )
            {
                $model->specialty()->sync($model->set_specialties);
            }
        });
    }
}

这是我从控制器的更新方法:

public function update($id)
{
    $data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id');

    $school = School::find($id);
    $school->name = $data['name'];
    $school->type_id = $data['type_id'];
    $school->set_specialties = $data['specialties'];
    $school->city_id = $data['city_id'];

    try {
        $school->save();
    } catch (ValidationException $errors) {
        return Redirect::route('admin.schools.edit', array($id))
            ->withErrors($errors->getErrors())
            ->withInput();
    }

    return Redirect::route('admin.schools.edit', array($id))
        ->withErrors(array('mainSuccess' => 'It's updated successful!'));
}

I need to listen for updating event in model but I need to put to model some variables that doesn't exist in table. I made it by adding public variables to model but when I have changes only on these 'fake' attributes I can't listen. Is there anyway to pass 'fake' attributes to model and can listen for updateing?

Here is my example class.

<?php

class School extends Eloquent {
    protected $table = 'schools';

    protected $fillable = array('name', 'type_id', 'city');
    protected $guarded = array('id');

    public $set_specialties;

    protected static function boot()
    {
        parent::boot();

        static::updating(function($model)
        {
            $data = array(
                'name' => $model->name,
                'type_id' => $model->type_id,
                'specialties' => $model->set_specialties,
                'city_id' => $model->city_id
            );

            $rules = array(
                'name' => 'required|min:3|max:50',
                'type_id' => 'required|min:1|max:300000',
                'specialties' => 'required|array',
                'city_id' => 'required|min:1|max:300000'
            );

            $validator = Validator::make($data, $rules);

            if ($validator->fails()) {
                throw new ValidationException(null, null, null, $validator->messages());
            } else {    
                return true;
            }
        });

        static::updated(function($model)
        {
            if ( $model->set_specialties != null )
            {
                $model->specialty()->sync($model->set_specialties);
            }
        });
    }
}

And here is my update method from controller:

public function update($id)
{
    $data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id');

    $school = School::find($id);
    $school->name = $data['name'];
    $school->type_id = $data['type_id'];
    $school->set_specialties = $data['specialties'];
    $school->city_id = $data['city_id'];

    try {
        $school->save();
    } catch (ValidationException $errors) {
        return Redirect::route('admin.schools.edit', array($id))
            ->withErrors($errors->getErrors())
            ->withInput();
    }

    return Redirect::route('admin.schools.edit', array($id))
        ->withErrors(array('mainSuccess' => 'It's updated successful!'));
}

原文:https://stackoverflow.com/questions/28439046
更新时间:2024-03-14 11:03

最满意答案

您是否尝试使用configSource元素从外部配置文件导入相关部分? 本讨论最后一个答案提供了一个可能适合您的解决方案。

引自该讨论:

<!-- WCF Configuration Mappings in app.config -->
<system.serviceModel>
  <bindings configSource=".\CommonConfig\ServiceBindings.config" />
  <client configSource=".\CommonConfig\ServiceClient.config" />
</system.serviceModel>

ServiceBindings.config:

<?xml version="1.0" encoding="utf-8" ?>
<bindings>
  <netTcpBinding>
    <binding ... />
  </netTcpBinding>
</bindings>

ServiceClient.config(缩写):

<?xml version="1.0" encoding="utf-8" ?>
<client>
  <endpoint ...>
      ...
  </endpoint>
</client>

Have you tried importing the relevant sections from an external config file using the configSource element? The last answer in this discussion provides a solution that may work for you.

Quoted from that discussion:

<!-- WCF Configuration Mappings in app.config -->
<system.serviceModel>
  <bindings configSource=".\CommonConfig\ServiceBindings.config" />
  <client configSource=".\CommonConfig\ServiceClient.config" />
</system.serviceModel>

ServiceBindings.config:

<?xml version="1.0" encoding="utf-8" ?>
<bindings>
  <netTcpBinding>
    <binding ... />
  </netTcpBinding>
</bindings>

ServiceClient.config (shortened):

<?xml version="1.0" encoding="utf-8" ?>
<client>
  <endpoint ...>
      ...
  </endpoint>
</client>

相关问答

更多
  • 您可以在bar/.htaccess使用此规则: RewriteEngine On RewriteRule ^page(/.*)?$ /foo/index.php [L,NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] You can use this rule in bar/.htaccess: RewriteEngine On Rewrite ...
  • 您是否尝试使用configSource元素从外部配置文件导入相关部分? 本讨论的最后一个答案提供了一个可能适合您的解决方案。 引自该讨论:
    这个值不会传递给你的单独函数。 实际上, this在你的函数中被设置为window对象。 您需要更改为这种类型的代码才能将正确的值传递给您的函数: 和你的代码: