关注点分离-通过使用服务控制2个从属实体?

时间:2019-02-28 18:24:08

标签: php laravel design-patterns service

我有两项服务:ProfileService知道如何操作Profile模型(雄辩的雄辩者)和ListService知道如何操纵List模型。 ProfileList具有一对多关系。在那些雄辩的模型中定义了关系。

我的问题是关注点分离。我打算让ProfileService仅对Profile做事,而ListService仅对List做事。

在遵守以下要求的同时,我需要做哪种设计模式或构造?

  • 我希望在destroyProfile对象上使用一种Profile方法,该方法可以删除Profile,并使其相关的List也被删除。

我想我需要ProfileService可以用来删除两个模型的第三项服务。

1 个答案:

答案 0 :(得分:2)

为什么不让数据库处理删除?

    Schema::table('lists', function (Blueprint $table) {
        $table->unsignedInteger('profile_id');
        $table->foreign('profile_id')
            ->references('id')
            ->on('profiles')
            ->onDelete('cascade');
    });