Yii2 relation duplicate when use inner join and jeft join












2















There are 3 Models with relation between themselves.
Example



class A extends ActiveRecord
{
public static function tableName(){
return 'tbl_a';
}
public function getB()
{
return $this->hasOne(B::className(), ['column' => 'column']);
}
}

class B extends ActiveRecord
{
public static function tableName(){
return 'tbl_b';
}
public function getC()
{
return $this->hasOne(C::className(), ['column' => 'column']);
}
}


I have next code:



$result = A::find()->joinWith('b')->where('');
if () {
A->joinWith('b.c')->where('');
}
$result->createCommand()->rawSql;


And outcome I have next sql:



select * from tbl_a left join tbl_b on ... join tbl_b on ... join tbl_c on ... where ...


As you can see sql query duplicates table relation 'tbl_b'. Do you know why?



UPDATE



OK, I researched my problem in more detail.
Table connection is duplicated if used different JOIN types.
Next original models:



class Myuser extends ActiveRecord
{
public static function tableName(){
return 'myuser';
}

public function getProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'id']);
}
}

class Profile extends yiidbActiveRecord {

public static function tableName(){
return 'profile';
}

public function getCity()
{
return $this->hasOne(City::className(), ['id'=>'city_id']);
}
}


Executed code:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` 
INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`


How can I avoid repetition, if I need to get a unique table 'profile' and add fields from the table 'city'. If there are no the fields in table 'city', then value should be 'null'.










share|improve this question

























  • Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

    – rob006
    Nov 15 '18 at 23:27











  • whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

    – Muhammad Omer Aslam
    Nov 16 '18 at 8:12











  • @rob006 I update my question.

    – Alex Mikitin
    Nov 16 '18 at 8:47
















2















There are 3 Models with relation between themselves.
Example



class A extends ActiveRecord
{
public static function tableName(){
return 'tbl_a';
}
public function getB()
{
return $this->hasOne(B::className(), ['column' => 'column']);
}
}

class B extends ActiveRecord
{
public static function tableName(){
return 'tbl_b';
}
public function getC()
{
return $this->hasOne(C::className(), ['column' => 'column']);
}
}


I have next code:



$result = A::find()->joinWith('b')->where('');
if () {
A->joinWith('b.c')->where('');
}
$result->createCommand()->rawSql;


And outcome I have next sql:



select * from tbl_a left join tbl_b on ... join tbl_b on ... join tbl_c on ... where ...


As you can see sql query duplicates table relation 'tbl_b'. Do you know why?



UPDATE



OK, I researched my problem in more detail.
Table connection is duplicated if used different JOIN types.
Next original models:



class Myuser extends ActiveRecord
{
public static function tableName(){
return 'myuser';
}

public function getProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'id']);
}
}

class Profile extends yiidbActiveRecord {

public static function tableName(){
return 'profile';
}

public function getCity()
{
return $this->hasOne(City::className(), ['id'=>'city_id']);
}
}


Executed code:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` 
INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`


How can I avoid repetition, if I need to get a unique table 'profile' and add fields from the table 'city'. If there are no the fields in table 'city', then value should be 'null'.










share|improve this question

























  • Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

    – rob006
    Nov 15 '18 at 23:27











  • whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

    – Muhammad Omer Aslam
    Nov 16 '18 at 8:12











  • @rob006 I update my question.

    – Alex Mikitin
    Nov 16 '18 at 8:47














2












2








2








There are 3 Models with relation between themselves.
Example



class A extends ActiveRecord
{
public static function tableName(){
return 'tbl_a';
}
public function getB()
{
return $this->hasOne(B::className(), ['column' => 'column']);
}
}

class B extends ActiveRecord
{
public static function tableName(){
return 'tbl_b';
}
public function getC()
{
return $this->hasOne(C::className(), ['column' => 'column']);
}
}


I have next code:



$result = A::find()->joinWith('b')->where('');
if () {
A->joinWith('b.c')->where('');
}
$result->createCommand()->rawSql;


And outcome I have next sql:



select * from tbl_a left join tbl_b on ... join tbl_b on ... join tbl_c on ... where ...


As you can see sql query duplicates table relation 'tbl_b'. Do you know why?



UPDATE



OK, I researched my problem in more detail.
Table connection is duplicated if used different JOIN types.
Next original models:



class Myuser extends ActiveRecord
{
public static function tableName(){
return 'myuser';
}

public function getProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'id']);
}
}

class Profile extends yiidbActiveRecord {

public static function tableName(){
return 'profile';
}

public function getCity()
{
return $this->hasOne(City::className(), ['id'=>'city_id']);
}
}


Executed code:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` 
INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`


How can I avoid repetition, if I need to get a unique table 'profile' and add fields from the table 'city'. If there are no the fields in table 'city', then value should be 'null'.










share|improve this question
















There are 3 Models with relation between themselves.
Example



class A extends ActiveRecord
{
public static function tableName(){
return 'tbl_a';
}
public function getB()
{
return $this->hasOne(B::className(), ['column' => 'column']);
}
}

class B extends ActiveRecord
{
public static function tableName(){
return 'tbl_b';
}
public function getC()
{
return $this->hasOne(C::className(), ['column' => 'column']);
}
}


I have next code:



$result = A::find()->joinWith('b')->where('');
if () {
A->joinWith('b.c')->where('');
}
$result->createCommand()->rawSql;


And outcome I have next sql:



select * from tbl_a left join tbl_b on ... join tbl_b on ... join tbl_c on ... where ...


As you can see sql query duplicates table relation 'tbl_b'. Do you know why?



UPDATE



OK, I researched my problem in more detail.
Table connection is duplicated if used different JOIN types.
Next original models:



class Myuser extends ActiveRecord
{
public static function tableName(){
return 'myuser';
}

public function getProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'id']);
}
}

class Profile extends yiidbActiveRecord {

public static function tableName(){
return 'profile';
}

public function getCity()
{
return $this->hasOne(City::className(), ['id'=>'city_id']);
}
}


Executed code:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` 
INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `profile` ON `myuser`.`id` = `profile`.`user_id`
LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`


How can I avoid repetition, if I need to get a unique table 'profile' and add fields from the table 'city'. If there are no the fields in table 'city', then value should be 'null'.







php sql yii2 relation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:33









Anton Rybalko

719917




719917










asked Nov 15 '18 at 20:03









Alex MikitinAlex Mikitin

133




133













  • Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

    – rob006
    Nov 15 '18 at 23:27











  • whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

    – Muhammad Omer Aslam
    Nov 16 '18 at 8:12











  • @rob006 I update my question.

    – Alex Mikitin
    Nov 16 '18 at 8:47



















  • Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

    – rob006
    Nov 15 '18 at 23:27











  • whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

    – Muhammad Omer Aslam
    Nov 16 '18 at 8:12











  • @rob006 I update my question.

    – Alex Mikitin
    Nov 16 '18 at 8:47

















Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

– rob006
Nov 15 '18 at 23:27





Can you show some real code here as an example? These code snippets are not valid PHP code, so it is hard to understand your question.

– rob006
Nov 15 '18 at 23:27













whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

– Muhammad Omer Aslam
Nov 16 '18 at 8:12





whats with A->joinWith('b.c')->where(''); tha doesnt seem to help

– Muhammad Omer Aslam
Nov 16 '18 at 8:12













@rob006 I update my question.

– Alex Mikitin
Nov 16 '18 at 8:47





@rob006 I update my question.

– Alex Mikitin
Nov 16 '18 at 8:47












2 Answers
2






active

oldest

votes


















0














I guess it's kind a bug (or feature) in the framework. When you use relation profile.city framework doosn't see relation already joined by inner join... I assume that if you use left join for first relation, everything will work fine.



In your case try to use leftJoin() and specify the table name to join:



$get_city = 1;
$u = Myuser::find()->innerJoinWith('profile');
if ($get_city) {
$u->leftJoin('city', 'profile.city_id = city.id');
}
echo $u->createCommand()->rawSql;





share|improve this answer


























  • You are right, I did it too. But I want to use the relation)

    – Alex Mikitin
    Nov 16 '18 at 9:45











  • So, it answer for me now.

    – Alex Mikitin
    Nov 16 '18 at 12:14



















0














This is because you're using different join type for these two joins. joinWith('profile', false, 'INNER JOIN') and joinWith('profile') will generate different JOIN (joinWith() uses LEFT JOIN as join type by default), so you have 2 joins you query. If you want to avoid duplicates, you may use the same settings for these two joinWith() calls:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` INNER JOIN `city` ON `profile`.`city_id` = `city`.`id`




If you want to combine INNER JOIN and LEFT JOIN, you may use extended syntax:



$get_city = 1;
if ($get_city) {
$u = Myuser::find()->joinWith(
[
'profile' => function (ActiveQuery $query) {
$query->joinWith('city');
},
],
false,
'INNER JOIN'
);
} else {
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`





share|improve this answer


























  • Yes, but I need use INNER JOIN only for first table..

    – Alex Mikitin
    Nov 16 '18 at 9:39











  • @AlexMikitin Check my update.

    – rob006
    Nov 16 '18 at 9:49











  • OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

    – Alex Mikitin
    Nov 16 '18 at 11:19













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327131%2fyii2-relation-duplicate-when-use-inner-join-and-jeft-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I guess it's kind a bug (or feature) in the framework. When you use relation profile.city framework doosn't see relation already joined by inner join... I assume that if you use left join for first relation, everything will work fine.



In your case try to use leftJoin() and specify the table name to join:



$get_city = 1;
$u = Myuser::find()->innerJoinWith('profile');
if ($get_city) {
$u->leftJoin('city', 'profile.city_id = city.id');
}
echo $u->createCommand()->rawSql;





share|improve this answer


























  • You are right, I did it too. But I want to use the relation)

    – Alex Mikitin
    Nov 16 '18 at 9:45











  • So, it answer for me now.

    – Alex Mikitin
    Nov 16 '18 at 12:14
















0














I guess it's kind a bug (or feature) in the framework. When you use relation profile.city framework doosn't see relation already joined by inner join... I assume that if you use left join for first relation, everything will work fine.



In your case try to use leftJoin() and specify the table name to join:



$get_city = 1;
$u = Myuser::find()->innerJoinWith('profile');
if ($get_city) {
$u->leftJoin('city', 'profile.city_id = city.id');
}
echo $u->createCommand()->rawSql;





share|improve this answer


























  • You are right, I did it too. But I want to use the relation)

    – Alex Mikitin
    Nov 16 '18 at 9:45











  • So, it answer for me now.

    – Alex Mikitin
    Nov 16 '18 at 12:14














0












0








0







I guess it's kind a bug (or feature) in the framework. When you use relation profile.city framework doosn't see relation already joined by inner join... I assume that if you use left join for first relation, everything will work fine.



In your case try to use leftJoin() and specify the table name to join:



$get_city = 1;
$u = Myuser::find()->innerJoinWith('profile');
if ($get_city) {
$u->leftJoin('city', 'profile.city_id = city.id');
}
echo $u->createCommand()->rawSql;





share|improve this answer















I guess it's kind a bug (or feature) in the framework. When you use relation profile.city framework doosn't see relation already joined by inner join... I assume that if you use left join for first relation, everything will work fine.



In your case try to use leftJoin() and specify the table name to join:



$get_city = 1;
$u = Myuser::find()->innerJoinWith('profile');
if ($get_city) {
$u->leftJoin('city', 'profile.city_id = city.id');
}
echo $u->createCommand()->rawSql;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 9:20

























answered Nov 16 '18 at 9:12









Anton RybalkoAnton Rybalko

719917




719917













  • You are right, I did it too. But I want to use the relation)

    – Alex Mikitin
    Nov 16 '18 at 9:45











  • So, it answer for me now.

    – Alex Mikitin
    Nov 16 '18 at 12:14



















  • You are right, I did it too. But I want to use the relation)

    – Alex Mikitin
    Nov 16 '18 at 9:45











  • So, it answer for me now.

    – Alex Mikitin
    Nov 16 '18 at 12:14

















You are right, I did it too. But I want to use the relation)

– Alex Mikitin
Nov 16 '18 at 9:45





You are right, I did it too. But I want to use the relation)

– Alex Mikitin
Nov 16 '18 at 9:45













So, it answer for me now.

– Alex Mikitin
Nov 16 '18 at 12:14





So, it answer for me now.

– Alex Mikitin
Nov 16 '18 at 12:14













0














This is because you're using different join type for these two joins. joinWith('profile', false, 'INNER JOIN') and joinWith('profile') will generate different JOIN (joinWith() uses LEFT JOIN as join type by default), so you have 2 joins you query. If you want to avoid duplicates, you may use the same settings for these two joinWith() calls:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` INNER JOIN `city` ON `profile`.`city_id` = `city`.`id`




If you want to combine INNER JOIN and LEFT JOIN, you may use extended syntax:



$get_city = 1;
if ($get_city) {
$u = Myuser::find()->joinWith(
[
'profile' => function (ActiveQuery $query) {
$query->joinWith('city');
},
],
false,
'INNER JOIN'
);
} else {
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`





share|improve this answer


























  • Yes, but I need use INNER JOIN only for first table..

    – Alex Mikitin
    Nov 16 '18 at 9:39











  • @AlexMikitin Check my update.

    – rob006
    Nov 16 '18 at 9:49











  • OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

    – Alex Mikitin
    Nov 16 '18 at 11:19


















0














This is because you're using different join type for these two joins. joinWith('profile', false, 'INNER JOIN') and joinWith('profile') will generate different JOIN (joinWith() uses LEFT JOIN as join type by default), so you have 2 joins you query. If you want to avoid duplicates, you may use the same settings for these two joinWith() calls:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` INNER JOIN `city` ON `profile`.`city_id` = `city`.`id`




If you want to combine INNER JOIN and LEFT JOIN, you may use extended syntax:



$get_city = 1;
if ($get_city) {
$u = Myuser::find()->joinWith(
[
'profile' => function (ActiveQuery $query) {
$query->joinWith('city');
},
],
false,
'INNER JOIN'
);
} else {
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`





share|improve this answer


























  • Yes, but I need use INNER JOIN only for first table..

    – Alex Mikitin
    Nov 16 '18 at 9:39











  • @AlexMikitin Check my update.

    – rob006
    Nov 16 '18 at 9:49











  • OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

    – Alex Mikitin
    Nov 16 '18 at 11:19
















0












0








0







This is because you're using different join type for these two joins. joinWith('profile', false, 'INNER JOIN') and joinWith('profile') will generate different JOIN (joinWith() uses LEFT JOIN as join type by default), so you have 2 joins you query. If you want to avoid duplicates, you may use the same settings for these two joinWith() calls:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` INNER JOIN `city` ON `profile`.`city_id` = `city`.`id`




If you want to combine INNER JOIN and LEFT JOIN, you may use extended syntax:



$get_city = 1;
if ($get_city) {
$u = Myuser::find()->joinWith(
[
'profile' => function (ActiveQuery $query) {
$query->joinWith('city');
},
],
false,
'INNER JOIN'
);
} else {
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`





share|improve this answer















This is because you're using different join type for these two joins. joinWith('profile', false, 'INNER JOIN') and joinWith('profile') will generate different JOIN (joinWith() uses LEFT JOIN as join type by default), so you have 2 joins you query. If you want to avoid duplicates, you may use the same settings for these two joinWith() calls:



$get_city = 1;
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
if ($get_city) {
$u->joinWith('profile.city', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` INNER JOIN `city` ON `profile`.`city_id` = `city`.`id`




If you want to combine INNER JOIN and LEFT JOIN, you may use extended syntax:



$get_city = 1;
if ($get_city) {
$u = Myuser::find()->joinWith(
[
'profile' => function (ActiveQuery $query) {
$query->joinWith('city');
},
],
false,
'INNER JOIN'
);
} else {
$u = Myuser::find()->joinWith('profile', false, 'INNER JOIN');
}
echo $u->createCommand()->rawSql;


Result:



SELECT `myuser`.* FROM `myuser` INNER JOIN `profile` ON `myuser`.`id` = `profile`.`user_id` LEFT JOIN `city` ON `profile`.`city_id` = `city`.`id`






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 9:49

























answered Nov 16 '18 at 9:33









rob006rob006

10.7k31436




10.7k31436













  • Yes, but I need use INNER JOIN only for first table..

    – Alex Mikitin
    Nov 16 '18 at 9:39











  • @AlexMikitin Check my update.

    – rob006
    Nov 16 '18 at 9:49











  • OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

    – Alex Mikitin
    Nov 16 '18 at 11:19





















  • Yes, but I need use INNER JOIN only for first table..

    – Alex Mikitin
    Nov 16 '18 at 9:39











  • @AlexMikitin Check my update.

    – rob006
    Nov 16 '18 at 9:49











  • OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

    – Alex Mikitin
    Nov 16 '18 at 11:19



















Yes, but I need use INNER JOIN only for first table..

– Alex Mikitin
Nov 16 '18 at 9:39





Yes, but I need use INNER JOIN only for first table..

– Alex Mikitin
Nov 16 '18 at 9:39













@AlexMikitin Check my update.

– rob006
Nov 16 '18 at 9:49





@AlexMikitin Check my update.

– rob006
Nov 16 '18 at 9:49













OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

– Alex Mikitin
Nov 16 '18 at 11:19







OK, what you will tell me about it. $get_city = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_city) { $u->joinWith('profile.city'); } $get_country = 1; $u = Myuser::find()->joinWith('profile', 0, 'INNER JOIN'); if ($get_country ) { $u->joinWith('profile.get_country'); } echo $u->createCommand()->rawSql;

– Alex Mikitin
Nov 16 '18 at 11:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327131%2fyii2-relation-duplicate-when-use-inner-join-and-jeft-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues