Laravel:复杂的条件和行动的许多多态关系

发布于 2025-02-14 00:56:06 字数 1012 浏览 1 评论 0原文

方法 模型是条件的容器(Triggers)和 Action

  • 方法有多个条件
  • 每个条件具有多个操作

我们可以拥有不同类型的条件,例如:

  • 订购金额 0500 USD
  • 订购权重 10 10代码>和100

,我们可以例如,有不同类型的操作,例如:


我们正在为每种类型的条件>条件和一个表创建一个表格对于每种类型的操作。这样一来,我们就不会最终得到一个装满列的表,以涵盖每个可能的情况。

问题是:如何建立这种多态关系?


Starting with Conditions

My database looks like:

The Method model is a container of conditions (triggers) and actions.

  • A Method has multiple Condition
  • Each Condition has multiple Action

We can have different types of Condition, for instance:

  • Order amount between 0 and 500 USD
  • Order weight between 10 and 100 pounds

And we can have different types of Action, for instance:


enter image description here

We are creating a table for each type of condition and a table for each type of action. That way we don't end up with a table full of columns to encompass each possible scenario.

The problem is: how to create this Polymorphic Relationship?


Starting with Conditions

My database looks like:

???? conditions
idcondition_idcondition_typemethod_id
10015005App\Model\ConditionTypeOrderAmount1
10026006App\Model\ConditionTypeOrderWeight1
condition_type_order_amount
idmin_amountmax_amount
50050500
condition_type_order_weight
idmin_weightmax_weight
600610100
???? actions
idaction_idaction_typecondition_id
80082002App\Models\ActionTypeCallSupervisor1001
80092002App\Models\ActionTypeCallSupervisor1002
action_type_call_supervisor
idsupervisor_idcall_anytime
200251

I could partially achieve what I need using belongsToMany and wherePivot in Method, like:

class Method extends Model
{
    public function orderAmountConditions()
    {
        return $this
            ->belongsToMany(
                ConditionTypeOrderAmount::class,
                'conditions',
                'method_id',
                'condition_id',
            )->wherePivot(
                'condition_type',
                ConditionTypeOrderAmount::class
            );
    }

    public function orderWeightConditions()
    {
        return $this
            ->belongsToMany(
                ConditionTypeOrderWeight::class,
                'conditions',
                'method_id',
                'condition_id',
            )->wherePivot(
                'condition_type',
                ConditionTypeOrderWeight::class
            );
    }
}

But in that scenario conditions table was used just as a pivot table and I had a single function for every type of condition. Not ideal.

So I tried to use Polymorphic Relationships, but than thing got confusing…

What I tried:

Both ConditionTypeOrderAmount and ConditionTypeOrderWeight have the same conditions() relationship:

class ConditionTypeOrderAmount extends Model
{
    public function conditions()
    {
        return $this->morphMany(
            Condition::class,
            'conditions'
        );
    }
}

And Condition has:

class Condition extends MorphPivot
{
    public function conditionable()
    {
        return $this->morphMany(
            Condition::class,
            'conditions',
            'condition_id',
        );
    }
}

But no results are found. I already read Laravel docs several times and watched a lot of YouTube videos, but I can't figure Polymorphic relationships.

Expected response would be something like:

{
  "method": {
    "id": 1,
    "conditions": [
      {
        "id": 1001,
        "type": "App\Models\ConditionalTypeOrderAmount",
        "min_amount": 0,
        "max_amount": 500,
        "actions": [
          {
            "id": 8008,
            "type": "App\Models\ActionTypeCallSupervisor",
            "supervisor_id": 5,
            "call_anytime": 1,
          }
        ]
      },
      {
        "id": 1002,
        "type": "App\Models\ConditionalTypeOrderWeight",
        "min_weight": 10,
        "max_weight": 100,
        "actions": [
          {
            "id": 8009,
            "type": "App\Models\ActionTypeCallSupervisor",
            "supervisor_id": 5,
            "call_anytime": 1,
          }
        ]
      }
    ]
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2025-02-21 00:56:06

条件模型(扩展

    public function conditionable()
    {
        return $this->morphTo();
    }

    public function method()
    {
        return $this->belongsTo(
            Method::class
        );
    }

    public function actions()
    {
        return $this->hasMany(
            Action::class
        );
    }

模型)中:

    public function conditions()
    {
        return $this->morphMany(
            Condition::class,
            'conditionable',
        );
    }

    public function conditions()
    {
        return $this->hasMany(
            Condition::class
        );
    }

在 中action模型(扩展模型):

    public function actionable()
    {
        return $this->morphTo();
    }

    public function condition()
    {
        return $this->belongsTo(
            Condition::class
        );
    }

actionType中....模型(扩展模型):

    public function actions()
    {
        return $this->morphMany(
            Action::class,
            'actionable',
        );
    }

In Condition model (extends Model):

    public function conditionable()
    {
        return $this->morphTo();
    }

    public function method()
    {
        return $this->belongsTo(
            Method::class
        );
    }

    public function actions()
    {
        return $this->hasMany(
            Action::class
        );
    }

In ConditionType.... models (extends Model):

    public function conditions()
    {
        return $this->morphMany(
            Condition::class,
            'conditionable',
        );
    }

In Method model (extends Model):

    public function conditions()
    {
        return $this->hasMany(
            Condition::class
        );
    }

In Action model (extends Model):

    public function actionable()
    {
        return $this->morphTo();
    }

    public function condition()
    {
        return $this->belongsTo(
            Condition::class
        );
    }

In ActionType.... models (extends Model):

    public function actions()
    {
        return $this->morphMany(
            Action::class,
            'actionable',
        );
    }
无需解释 2025-02-21 00:56:06

我不知道这是否会解决问题,但是我将通过提取这些数据本身的SQL开始解决问题。如果您设法正确格式化了数据,则可能会自己解决问题

select
    i.id,
    i.condition_type,
    ct.min_amount,
    ct.max_amount,
    atc.supervisor_id,
    atc.call_anytime
from
    conditions i
    left join condition_type_order_amount ct on i.condition_id = ct.id
    left join actions a on i.id = a.condition_id
    left join action_type_call_supervisor atc on a.action_id = atc.id
where
    i.condition_type = 'ConditionalTypeOrderAmount';

I don't know if this will solve the problem, but I would start solving the problem by extracting sql of this data itself. If you manage to format the data correctly, you will probably fix the problem yourself

select
    i.id,
    i.condition_type,
    ct.min_amount,
    ct.max_amount,
    atc.supervisor_id,
    atc.call_anytime
from
    conditions i
    left join condition_type_order_amount ct on i.condition_id = ct.id
    left join actions a on i.id = a.condition_id
    left join action_type_call_supervisor atc on a.action_id = atc.id
where
    i.condition_type = 'ConditionalTypeOrderAmount';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文