duanbi1888 2014-05-14 08:50
浏览 60
已采纳

Laravel多态关系

What is the best method to handle an instance where there is no record for the polymorphic object.

   user_type A table ( user_id , username )

   user_type B table (user_id , username )

   event table (event_id, user_id, user_type )

Sample Data

A_table user_id , username 1 , sam 2 , john

B_table user_id, username 1, Don 2, Mike

Event_table event_id, user_id, user_type 1,1,A // Record 1 2,2,B // Record 2 1,5,A // Record 3

Consider the following pseudo code :
Event instance ->user->username

1) Running the above for Record 1 Should result in "Sam" 2) Running the above for Record 2 Should result in "Mike"

Imagine a 3rd case :

Where as "Event instance ->user" results in NULL.

How do we best tackle a situation like this apart from having to always check if the Object is null or not before every instance we use it.

  • 写回答

1条回答 默认 最新

  • dta25920 2014-06-20 18:13
    关注

    The best method to go around this was to do a simple :

    if ( ! $this->polymorfphic_object_method)
    {
      // skip now
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试