문제

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.

도움이 되었습니까?

해결책

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

if ( ! $this->polymorfphic_object_method)
{
  // skip now
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top