douye4254 2015-09-11 13:23
浏览 51
已采纳

Laravel 5.1 - 相对模型的模型观察者

Let's suppose I have the following related tables:

pages
----------------
id 
content

visitors
----------------
id
name

page_visitor
----------------
page_id
visitor_id

As you see can we have three tables, in which pages and visitors have a many-to-many relationship.

I have successfully implemented an observer class on the pages model class, and now, what ever I do to the pages table gets reflected somewhere else. Using the following code:

class ElasticsearchPageObserver
{
    private $elasticsearch;

    public function __construct(ESClient $client)
    {
        $this->elasticsearch = $client;
    }

    public function created(Page $page)
    {
        $params = $page->buildElasticsearchParams();
        $response = $this->elasticsearch->index($params);
    }

    public function updated(Page $page)
    {
        $params = $page->buildElasticsearchParams();
        $response = $this->elasticsearch->index($params);
    }
}

You get the idea, right?

As it turns out, when the tables that have a one-to-many relationship with the page table get updated, the above observer, observes it. But not for many-to-many relationships(like the above).

Well, now how do I go about this??? How to do the same thing when page_visitor gets updated?

Thanks

  • 写回答

1条回答 默认 最新

  • dourong9253 2015-09-11 15:26
    关注

    I ended up creating a method to use instead of calling attach on every related class.

    // in page:
    public function attachTo($to, $id)
        {
            switch($to) {
                case 'App\Models\Visitor':
                    $this->visitors()->attach($id);
                    break;
                case 'App\Models\Banner':
                    $this->banners()->attach($id);
                    break;
                case 'App\Models\AdBlock':
                    $this->adblocks()->attach($id);
                    break;
                default:
                    return;
            }
            $this->indexInElasticsearch();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?