保存的方法正在运行迁移时出错

时间:2018-11-18 08:19:10

标签: elasticsearch laravel-5 eloquent

在laravel 5.7应用程序中,我使用Elasticsearch,在模型中,我使用相关模型的保存和删除方法:

<?php

namespace App;

use DB;
use App\MyAppModel;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class Vote extends MyAppModel
{
    protected $table = 'votes';
    protected $elasticsearch_type  = 'vote';
    protected $primaryKey = 'id';
    public $timestamps = false;

    public function getTableName(): string
    {
        return $this->table;
    }



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

        static::deleted(function ($vote ) {
            // return; // TO UNCOMMENT
            $elastic = app(\App\Elastic\Elastic::class);
            $elasticsearch_root_index  = config('app.elasticsearch_root_index');
            $elasticsearch_type        = with(new Vote)->getElasticsearchType();
            $elastic->delete([
                'index' => $elasticsearch_root_index,
                'type' => $elasticsearch_type,
                'id' => $vote->id,
            ]);
        });

        static::saved(function ($vote) {
            // return; // TO UNCOMMENT
            $elastic = app(\App\Elastic\Elastic::class);
            $elasticsearch_root_index  = config('app.elasticsearch_root_index');
            $elasticsearch_type        = with(new Vote)->getElasticsearchType();

            $elastic->delete([
                'index' => $elasticsearch_root_index,
                'type'  => $elasticsearch_type,
                'id'    => $vote->id,
            ]);

            if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
                $elastic->index([
                    'index' => $elasticsearch_root_index,
                    'type'  => $elasticsearch_type,
                    'id'    => $vote->id,
                    'body'  => [
                        'id'          => $vote->id,
                        'slug'        => $vote->slug,
                        'name'        => $vote->name,
                        'description' => $vote->description,
                    ]
                ]);
            } // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch

当我在编辑器中修改数据时,它可以正常运行,但是运行种子程序命令

php artisan migrate:refresh --seed -v

我遇到了错误:

...
Migrating: 2018_07_13_150151_create_votes_table

   Elasticsearch\Common\Exceptions\Missing404Exception  : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

  at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
    603|             $exception = new AlreadyExpiredException($responseBody, $statusCode);
    604|         } elseif ($statusCode === 403) {
    605|             $exception = new Forbidden403Exception($responseBody, $statusCode);
    606|         } elseif ($statusCode === 404) {
  > 607|             $exception = new Missing404Exception($responseBody, $statusCode);
    608|         } elseif ($statusCode === 409) {
    609|             $exception = new Conflict409Exception($responseBody, $statusCode);
    610|         } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
    611|             $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

  Exception trace:

  1   Elasticsearch\Connections\Connection::process4xxError([])
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

  2   Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

  3   React\Promise\FulfilledPromise::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

  4   GuzzleHttp\Ring\Future\CompletedFutureValue::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

  5   GuzzleHttp\Ring\Core::proxy(Object(GuzzleHttp\Ring\Future\CompletedFutureArray), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

  6   Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}(Object(Elasticsearch\Connections\Connection), Object(Elasticsearch\Transport), [])
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

  7   Elasticsearch\Connections\Connection::performRequest("DELETE", "/select_vote/vote/2", [], [], Object(Elasticsearch\Transport))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

  8   Elasticsearch\Transport::performRequest("DELETE", "/select_vote/vote/2", [], [])
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

  9   Elasticsearch\Client::performRequest(Object(Elasticsearch\Endpoints\Delete))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

  10  Elasticsearch\Client::delete([])
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

  11  App\Elastic\Elastic::delete(["select_vote", "vote"])
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

  12  App\Vote::App\{closure}(Object(App\Vote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

  13  Illuminate\Events\Dispatcher::Illuminate\Events\{closure}("eloquent.saved: App\Vote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

  14  Illuminate\Events\Dispatcher::dispatch("eloquent.saved: App\Vote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

  15  Illuminate\Database\Eloquent\Model::fireModelEvent("saved")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

  16  Illuminate\Database\Eloquent\Model::finishSave([])
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

  17  Illuminate\Database\Eloquent\Model::save()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

  18  Illuminate\Database\Eloquent\Builder::Illuminate\Database\Eloquent\{closure}(Object(App\Vote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

  19  tap(Object(App\Vote), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

  20  Illuminate\Database\Eloquent\Builder::create()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

  21  Illuminate\Database\Eloquent\Model::forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), "create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

  22  Illuminate\Database\Eloquent\Model::__call("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

  23  Illuminate\Database\Eloquent\Model::__callStatic("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

  24  votesInitData::run()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

  25  call_user_func_array([])
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

...

如果在已保存和已删除的方法中取消注释,则返回正常工作(无需应付elasticsearch)。 为什么会出现错误以及如何解决? 有没有一种方法可以检查是否从迁移命令中调用它并退出?这可以解决这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

在类似情况下,我会在Migrations脚本开头的设置表中设置标记,例如

ax = {subplot(211), subplot(212)};

plot(ax{1}, rand(100,1));
plot(ax{2}, rand(100,1));

for i=1:numel(ax)
    ax{i}.Title.Fontsize = 14;
end

在上一个迁移脚本中,我更改了密钥

DB::table('settings')->insert([
    'name' => 'elastic_automation',
    'value' =>  'N',
]);

,然后在模型事件中检查此键。对我有用!