With() Doesn't Perform A Join

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?

http://www.yiiframework.com/forum/index.php/topic/43458-yii2-ar-join-dropped/

Hmm ok, so if i want to add a condition that uses both ‘user’ and ‘reginfo’ table, i need to write my own join?

yep.

Btw how to get joined table columns?

You can’t.

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.