Davey
(Davey975)
December 17, 2013, 2:51pm
1
The following code executes 2 seperate queries:
$test = User::find()->with('regInfo')->one();
echo $test->regInfo->activationKey;
User model:
public function getRegInfo()
{
return $this->hasOne(UserRegInfo::className(), ['userId' => 'id']);
}
Queries executed:
SELECT * FROM `user`
SELECT * FROM `userreginfo` WHERE `userId`='40'
Why doesn’t this create a join instead of executing two seperate queries? Do i need to specify the join manually?
fragoulas
(John Fragkoulis)
December 17, 2013, 2:58pm
2
Davey
(Davey975)
December 17, 2013, 3:11pm
3
Hmm ok, so if i want to add a condition that uses both ‘user’ and ‘reginfo’ table, i need to write my own join?
CeBe:
yep.
Btw how to get joined table columns?
CeBe
December 17, 2013, 8:57pm
7
Sure you can select them. With asArray() they will be part of the result. When you have AR they will be assigned to a property of AR that you have to define first.
See AR::create() method on how it works.