Laravel:复杂的条件和行动的许多多态关系
方法
模型是条件的容器(Triggers)和 Action 。
方法
有多个条件
- 每个
条件
具有多个操作
我们可以拥有不同类型的条件,例如:
- 订购金额
0
和500
USD - 订购权重
10 10
代码>和100
磅
,我们可以例如,有不同类型的操作
,例如:
- 呼叫主管ID
5
- 将电子邮件发送到
[email  pretanced]
我们正在为每种类型的条件>条件
和一个表创建一个表格对于每种类型的操作
。这样一来,我们就不会最终得到一个装满列的表,以涵盖每个可能的情况。
问题是:如何建立这种多态关系?
Starting with Conditions
…
My database looks like:
The Method
model is a container of conditions (triggers) and actions.
- A
Method
has multipleCondition
- Each
Condition
has multipleAction
We can have different types of Condition
, for instance:
- Order amount between
0
and500
USD - Order weight between
10
and100
pounds
And we can have different types of Action
, for instance:
- Call supervisor ID
5
- Send email to
[email protected]
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
id | condition_id | condition_type | method_id |
---|---|---|---|
1001 | 5005 | App\Model\ConditionTypeOrderAmount | 1 |
1002 | 6006 | App\Model\ConditionTypeOrderWeight | 1 |
condition_type_order_amount
id | min_amount | max_amount |
---|---|---|
5005 | 0 | 500 |
condition_type_order_weight
id | min_weight | max_weight |
---|---|---|
6006 | 10 | 100 |
???? actions
id | action_id | action_type | condition_id |
---|---|---|---|
8008 | 2002 | App\Models\ActionTypeCallSupervisor | 1001 |
8009 | 2002 | App\Models\ActionTypeCallSupervisor | 1002 |
action_type_call_supervisor
id | supervisor_id | call_anytime |
---|---|---|
2002 | 5 | 1 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
条件
模型(扩展模型)中:
在 中
action
模型(扩展模型):在
actionType中....
模型(扩展模型):In
Condition
model (extends Model):In
ConditionType....
models (extends Model):In
Method
model (extends Model):In
Action
model (extends Model):In
ActionType....
models (extends Model):我不知道这是否会解决问题,但是我将通过提取这些数据本身的SQL开始解决问题。如果您设法正确格式化了数据,则可能会自己解决问题
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