Closed m24km closed 2 years ago
Does it help if you alias the main model and specify that alias for id
in the condition? This is interesting case.
Oh, and please check it on the current Yii version, maybe it is already taken care of.
I updated yii to 2.044 I used alias for main model and used joinWith in different test case with alias for main model and related model and I am getting same results for this specific data set ( but correct result returned when: city1 and city2 belong to different Countries OR district1 and district2 belong to same City)
I have tried with this case, via latest dev-master (2.0.45, php 8.1.1) and it gives the expected results, the 2 country objects.
when use the query
$subDistricts = SubDistrict::find()
->with('country')
->where(['in', 'id', [1, 2]])
->all();
and then extract the country from each subDistrict $subDistrict[0]->country
..
Following are some relations inside SubDistrict class
...
class SubDistrict extends ActiveRecord
{
public function getDistrict()
{
return $this->hasOne(District::class, ['id' => 'district_id']);
}
public function getCity()
{
return $this->hasOne(City::class, ['id' => 'city_id'])->via('district');
}
public function getCountry()
{
return $this->hasOne(Country::class, ['id' => 'country_id'])->via('city');
}
}
I forgot to ask which DB is it.
I used Mysql through debugging my project I tried to figure out why I am getting this results I found implementation of mapVia function drop some links
private function mapVia($map, $viaMap) {
$resultMap = [];
foreach ($map as $key => $linkKeys) {
foreach (array_keys($linkKeys) as $linkKey) {
$resultMap[$key] = $viaMap[$linkKey];
}
}
return $resultMap;
}
The following implementation return correct result for the case illustrated in issue;
private function mapVia($map, $viaMap)
{
$resultMap = [];
foreach ($map as $key => $linkKeys) {
$oneLinkMap = [];
foreach (array_keys($linkKeys) as $linkKey) {
foreach ($viaMap[$linkKey] as $k => $v) {
$oneLinkMap [$k] = $v;
}
}
$resultMap[$key] = $oneLinkMap;
}
return $resultMap;
}
@m24km so, is it fixed in the current dev-master like @longthanhtran mentioned?
No ,I have not seen or tested dev-master
It has been 2 or more weeks with no response on our request for more information. In order for our issue tracker to be effective, we are closing this issue.
If you want it to be reopened again, feel free to supply us with the requested information.
Thanks!
This is an automated comment, triggered by adding the label expired
.
What steps will reproduce the problem?
Given 4 activeRecord (Country, City, District, SubDistrict)with relation Country has many City (City hasOne Country) City has many District ( District hasOne City) District has many SubDistrict ( SubDistrict hasOne District )
Define getCountry() in SubDistrict Class like that :
run command to :
$result = SubDistrict::find()->with('country')->where(['in','id',[1,2])->all();
What is the expected result?
$result[0]->country = {coutry object} $result[1]->country = {coutry object}
What do you get instead?
$result[0]->country = null $result[1]->country = {coutry object}
Additional info
mapVia($map, $viaMap) in ActiveRelationTrait return incorrect link array (it drop some links)