tklustig
(Kipp Thomas)
October 26, 2017, 8:16am
1
hi guys, following query in plain sql will give me result as desired…
SELECT nachname FROM person LEFT JOIN e_kontakt ON person.id=e_kontakt.id_person where person.id=e_kontakt.id_person order by nachname;
last name:
Smith
Wilson
I want to get this values in a DropDownbox,but I get result(no error): No matching found. What do I wrong?
public static function nachname_for_ekontakt() {
$nachname = Person::find()
->leftJoin('e_kontakt', 'person.id=e_kontakt.id_person')
->where(['person.id' => 'e_kontakt.id_person'])->orderBy(['nachname' => SORT_ASC])->asArray()->all();
return yii\helpers\ArrayHelper::map($nachname, 'id', 'nachname');
}
Why don’t you use an INNER JOIN instead LEFT JOIN? Because in where condition you apply the same condition expressed in ‘on’.
SELECT nachname FROM person INNER JOIN e_kontakt ON person.id=e_kontakt.id_person order by nachname;
public static function nachname_for_ekontakt() {
$nachname = Person::find()
->innerJoin('e_kontakt', 'person.id=e_kontakt.id_person')
->orderBy(['nachname' => SORT_ASC])->asArray()->all();
return yii\helpers\ArrayHelper::map($nachname, 'id', 'nachname');
}
tklustig
(Kipp Thomas)
October 26, 2017, 10:40am
3
Why don’t you use an INNER JOIN instead LEFT JOIN? Because in where condition you apply the same condition expressed in ‘on’.
SELECT nachname FROM person INNER JOIN e_kontakt ON person.id=e_kontakt.id_person order by nachname;
public static function nachname_for_ekontakt() {
$nachname = Person::find()
->innerJoin('e_kontakt', 'person.id=e_kontakt.id_person')
->orderBy(['nachname' => SORT_ASC])->asArray()->all();
return yii\helpers\ArrayHelper::map($nachname, 'id', 'nachname');
}
Bingo. U were right. I didn’t bring in mind different JOINs in this case. Nevertheless, I get surprised that ur solution works, 'cause phpmyadmin works with both JOINS,yii
only with INNER JOIN. I know difference betwenn INNER JOIN and LEFT JOIN. so my final question is following:
Why will yii distinguish INNER and LEFT JOIN, but phpmyadmin will not? Both is PHP…
Why would not phpmyadmin distinguish INNER and LEFT JOIN?
tklustig
(Kipp Thomas)
October 26, 2017, 10:53am
5
It will distinguish, but phpmyadmin will accept both JOINs in my case, but yii won’t. Why?
I think that the error was in the code, because I don’t think that Yii distinguishes this automatically.
tklustig
(Kipp Thomas)
October 27, 2017, 6:34pm
7
All right. Thx a lot for ur help