Php 多多态域模型
我需要在我的系统中添加一个功能,这是一个库存控制系统 要添加的功能是关于在不同型号中启动聊天的可能性。我有产品、销售商、产品集合和更多的对象,我需要在其中开始对话 因此,我考虑用三个多态字段创建一个表,以使其完全可用于任何变体。这是田地Php 多多态域模型,php,laravel,eloquent,polymorphic-associations,Php,Laravel,Eloquent,Polymorphic Associations,我需要在我的系统中添加一个功能,这是一个库存控制系统 要添加的功能是关于在不同型号中启动聊天的可能性。我有产品、销售商、产品集合和更多的对象,我需要在其中开始对话 因此,我考虑用三个多态字段创建一个表,以使其完全可用于任何变体。这是田地 sender_id sender_type topic_id topic_type receiver_id receiver_type 以及使对话成为可能的适当字段消息、回复等 发送方和接收方需要是多态的,因为可以在系统用户和客户之间进行对话 我这样做对吗
sender_id
sender_type
topic_id
topic_type
receiver_id
receiver_type
以及使对话成为可能的适当字段消息、回复等
发送方和接收方需要是多态的,因为可以在系统用户和客户之间进行对话
我这样做对吗?这样行吗?另外,我不知道如何保存聊天实体。如果您想为多个发件人、收件人和主题设置聊天,我相信这种关系很好 此外,我无法理解您所说的聊天实体的确切含义,但下面的方法应该可以消除您对这种方法的任何疑虑 下面是你如何完成事情的方法 建立关系 按以下方式设置关系
class Chat
{
public function sender()
{
return $this->morphTo();
}
public function topic()
{
return $this->morphTo();
}
public function receiver()
{
return $this->morphTo();
}
}
class SystemUser
{
public function sentChats()
{
return $this->morphMany('App\Chat', 'sender');
}
public function receivedChats()
{
return $this->morphMany('App\Chat', 'receiver');
}
}
class Customer
{
public function sentChats()
{
return $this->morphMany('App\Chat', 'sender');
}
public function receivedChats()
{
return $this->morphMany('App\Chat', 'receiver');
}
}
class Illustrate
{
public function illustrable()
{
return $this->morphTo();
}
public function chats()
{
return $this->morphMany('App\Chat', 'topic');
}
}
创建聊天室
如何创建聊天室
public function create()
{
$inputs = request()->only([
'sender_id', 'sender_type', 'receiver_id', 'receiver_type', 'topic_id', 'topic_type',
'message', 'in_reply_of',
]);
$sender = $this->getSender($inputs);
$receiver = $this->getReceiver($inputs);
$topic = $this->getTopic($inputs);
if($sender && $receiver && $topic) {
$chat = $sender->sentChats()->create([
'receiver_id' => $receiver->id,
'receiver_type' => get_class($receiver),
'topic_id' => $topic->id,
'topic_type' => get_class($topic),
'message' => $inputs['message'],
'in_reply_of' => $inputs['in_reply_of'],
]);
}
}
private function getSender($inputs)
{
if(isset($inputs['sender_type'], $inputs['sender_id']) && is_numeric($inputs['sender_id'])) {
switch($inputs['sender_type']) {
case 'SystemUser':
return SystemUser::find($inputs['sender_id']);
case 'Customer':
return Customer::find($inputs['sender_id']);
default:
return null;
}
}
}
private function getReceiver($inputs)
{
if(isset($inputs['receiver_type'], $inputs['receiver_id']) && is_numeric($inputs['receiver_id'])) {
switch($inputs['receiver_type']) {
case 'SystemUser':
return SystemUser::find($inputs['receiver_id']);
case 'Customer':
return Customer::find($inputs['receiver_id']);
default:
return null;
}
}
}
private function getTopic($inputs)
{
if(isset($inputs['topic_type'], $inputs['topic_id']) && is_numeric($inputs['topic_id'])) {
switch($inputs['topic_type']) {
case 'Illustrate':
return Illustrate::find($inputs['topic_id']);
default:
return null;
}
}
}
聊天
注意:-请理解,我没有测试过这些。。。这只是一个关于如何完成事情的小例子
如果您在理解这一点时遇到任何问题,请告诉我 系统用户和客户是否有其他共同关系?主题id中将使用的其他模型如何。我想使用主题id和主题类型来保存任何对象,因此我无法将聊天功能添加到系统中的任何模型中。我在上一个问题中询问的常见关系如何?哦,抱歉,不,它们没有任何常见关系。一个是登录内联网系统,另一个是给客户的。最后一个问题。。。你说的如何保存聊天实体是什么意思?太棒了。谢谢你的时间。@Alan很高兴我能帮上忙:
public function get($id) {
$chat = Chat::find($id);
$sender = $chat->sender;
// Inverse
// $systemUser = SystemUser::find($id);
// $systemUser->sentChats->where('id', $chatId);
$receiver = $chat->receiver;
// Inverse
// $customer = Customer::find($id);
// $customer->receivedChats->where('id', $chatId);
$topic = $chat->topic;
// Inverse
// $illustrate = Illustrate::find($id);
// $illustrate->chats;
}