PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] yii hasone relation with condition

 December 30, 2021     activerecord, relationship, yii, yii2     No comments   

Issue

I have a hasone relation in my code,

public function getDevice(){
    return $this->hasOne(UserDevice::className(),['user_id' => 'id'])->where(['is_latest'=>true]);
}

In my query, I am trying to fetch user details.

  $query = User::find();
  $query->joinWith('device')->all();

If i am using getDevice without where condition, I am getting all users. But if I add where condition inside getDevice, I am getting users that have device details. Is there anyway to list all users with the chceking. That is I need to list all users, who has either details in device table or not


Solution

I have used this in my relation and it works.

public function getDevice(){
    return $this->hasOne(UserDeviceInfo::className(),['user_id' => 'id'])->orOnCondition(['is_latest' =>1]);
}


Answered By - reshma
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing