Query builder works only when chained instantly? i cant get working result if i split applying methods to PHQB in different parts of code.
This code works:
if ($this->request->isPost() && $this->request->isAjax()) {
switch ($this->request->getPost('type')) {
case 'getEmergencyCities':
$district_id = $this->request->getPost('district_id', 'int');
$result['e_objects_cities'] = Cities::findByDistrictId($district_id)->toArray();
$emergency_objects = $this->modelsManager->createBuilder()->columns([
'o.id',
'c.value AS city',
'a.value AS address',
'o.house_number',
'o.flats_count',
'o.area'
])
->from(['o' => 'Main\Models\EmergencyObjects'])
->join('Main\Models\Addresses', 'o.address_id = a.id', 'a')
->join('Main\Models\Cities', 'o.city_id = c.id', 'c')
->where('o.district_id = :district_id:', ['district_id' => $district_id])
->getQuery()->execute()->toArray();
break;
case 'getEmergencyAddresses':
$city_id = $this->request->getPost('city_id', 'int');
$result['e_objects_addresses'] = Addresses::findByCityId($city_id)->toArray();
$emergency_objects = $this->modelsManager->createBuilder()->columns([
'o.id',
'c.value AS city',
'a.value AS address',
'o.house_number',
'o.flats_count',
'o.area'
])
->from(['o' => 'Main\Models\EmergencyObjects'])
->join('Main\Models\Addresses', 'o.address_id = a.id', 'a')
->join('Main\Models\Cities', 'o.city_id = c.id', 'c')
->where('o.city_id = :city_id:', ['city_id' => $city_id])
->getQuery()->execute()->toArray();
break;
case 'getEmergencyObjects':
$address_id = $this->request->getPost('address_id');
$emergency_objects = $this->modelsManager->createBuilder()->columns([
'o.id',
'c.value AS city',
'a.value AS address',
'o.house_number',
'o.flats_count',
'o.area'
])
->from(['o' => 'Main\Models\EmergencyObjects'])
->join('Main\Models\Addresses', 'o.address_id = a.id', 'a')
->join('Main\Models\Cities', 'o.city_id = c.id', 'c')
->where('o.address_id = :address_id:', ['address_id' => $address_id])
->getQuery()->execute()->toArray();
break;
}
$result['emergency_objects'] = $emergency_objects;
echo json_encode($result, JSON_NUMERIC_CHECK);
}
And this doesnt:
if ($this->request->isPost() && $this->request->isAjax()) {
$emergency_objects = $this->modelsManager->createBuilder()->columns([
'o.id',
'c.value AS city',
'a.value AS address',
'o.house_number',
'o.flats_count',
'o.area'
])
->from(['o' => 'Main\Models\EmergencyObjects'])
->join('Main\Models\Addresses', 'o.address_id = a.id', 'a')
->join('Main\Models\Cities', 'o.city_id = c.id', 'c');
switch ($this->request->getPost('type')) {
case 'getEmergencyCities':
$district_id = $this->request->getPost('district_id', 'int');
$result['e_objects_cities'] = Cities::findByDistrictId($district_id)->toArray();
$emergency_objects->where('o.district_id = :district_id:', ['district_id' => $district_id]);
break;
case 'getEmergencyAddresses':
$city_id = $this->request->getPost('city_id', 'int');
$result['e_objects_addresses'] = Addresses::findByCityId($city_id)->toArray();
$emergency_objects->where('o.city_id = :city_id:', ['city_id' => $city_id]);
break;
case 'getEmergencyObjects':
$address_id = $this->request->getPost('address_id');
$emergency_objects->where('o.address_id = :address_id:', ['address_id' => $address_id]);
break;
}
$emergency_objects->getQuery()->execute()->toArray();
$result['emergency_objects'] = $emergency_objects;
echo json_encode($result, JSON_NUMERIC_CHECK);
}
Last one returns PHQB object, w/o applied $emergency_objects->getQuery()->execute()->toArray();
Anyone know whats the problem?