Z
Z
zagamay_ru2018-05-04 10:40:42
elasticsearch
zagamay_ru, 2018-05-04 10:40:42

How to correctly trigger an event?

Good afternoon!
There was a situation in the code.
There is a table news , news_tags , tags
I have news displayed through elasticsearch and there is a global event for listening to the update of this model in order to rebuild the updated data in elasticsearch for any actions in this model.

\Event::listen(
            ['eloquent.updated*', 'eloquent.created*', 'eloquent.deleted*'],
            function ($event, $params) {
                list($model) = $params;

                if ($model instanceof ModelSearch) {
                    /** @var ElasticSearchService $service */
                    $service = app()->make(ElasticSearchService::class);

                    if (strpos($event, 'eloquent.updated') === 0) {
                        $service->update($model);
                    }

                    if (strpos($event, 'eloquent.created') === 0) {
                        $service->add($model);
                    }

                    if (strpos($event, 'eloquent.deleted') === 0) {
                        $service->delete($model);
                    }
                }
            }
        );

But when I bind tags to this model, they are stored in an intermediate table and the news model itself does not react in any way.
How can I force it to update?
I see several solutions here:
  1. Update updated_at of the news, thereby triggering an event
  2. Trigger an event manually \Event::fire('eloquent.updated: ' . get_class($news), [$news]);

Can eat more correct decisions of the given situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Varyukhin, 2018-05-04
@zagamay_ru

There is such a thing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question