Yii Framework Forum: How To Return Empty Result In Has_One Relationship - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Return Empty Result In Has_One Relationship Rate Topic: -----

#1 User is offline   Ravi Verma 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 14-May 12

Posted 09 November 2012 - 06:19 AM

Hi Guys - I am having below relationship in my Model:


'userAverageTime' => array(self::HAS_ONE, 'UserStatistics', 'user_id','condition'=>"userAverageTime.key='AVERAGE_TIME'")



It is working perfectly fine if we are having result for a particular user in "UserStatistics" table. But if we don't have any result in userStatistics table for any user than no result for that user is being shown. I just want an option to show results with blank userStatistics object or without userStatistics object. But it is making an INNER JOIN for userStatistics object. I have tried below options also:


Below options are not working for me:
'userAverageTime' => array(self::HAS_ONE, 'UserStatistics', 'user_id','condition'=>"userAverageTime.key='AVERAGE_TIME'",'joinType'=>'LEFT OUTER JOIN'),

'userAverageTime' => array(self::HAS_MANY, 'UserStatistics', 'user_id','condition'=>"userAverageTime.key='AVERAGE_TIME'"),


'userAverageTime' => array(self::HAS_MANY, 'UserStatistics', 'user_id','condition'=>"userAverageTime.key='AVERAGE_TIME'",'joinType'=>'LEFT OUTER JOIN'),



Can anyone suggest something?

Thanks,
Ravi Verma
0

#2 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 771
  • Joined: 02-July 10
  • Location:Central Poland

Posted 09 November 2012 - 07:00 AM

$stats = $user->userAverageTime;
if( $stats === null ) {
  $stats = new UserStatistics();
}
...


this should do the trick.
red
0

#3 User is offline   Ravi Verma 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 14-May 12

Posted 19 November 2012 - 02:12 AM

Hi RedGuy: Thanks for your reply. Actually I'm using this relationship in Query. So my query is not returning me all those users which are not having any data in UserStatistics Table. I can use above trick but in that case, I need to loop all the results and check if they are having any data in userStatistics or not. That would not be good idea for large data sets.

Can u please suggest something else?

Thanks,
0

#4 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 771
  • Joined: 02-July 10
  • Location:Central Poland

Posted 19 November 2012 - 02:49 AM

$results = User::model()->with( 'userAverageTime' )->findAll( array(
  'condition'=>'userAverageTime.user_id IS NULL'
) );

is that what you want? relations are by default joined with 'OUTER JOIN' so when related record does not exists - there will be null value.
red
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users