Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PHQB - works only when chained?

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?

And this doesnt:

Are you getting an error or something?



382
Accepted
answer

Switch:

  $emergency_objects->getQuery()->execute()->toArray();
  $result['emergency_objects'] = $emergency_objects;

to:

  $result['emergency_objects'] = $emergency_objects->getQuery()->execute()->toArray();


475

Thanks, that fix worked

Switch:

``` $emergencyobjects->getQuery()->execute()->toArray(); $result['emergencyobjects'] = $emergency_objects; ```

to:

``` $result['emergencyobjects'] = $emergencyobjects->getQuery()->execute()->toArray(); ```



475

No, there was no error

And this doesnt:

Are you getting an error or something?



475
edited Aug '15

posting code just in case someone will google something similar

        if ($this->request->isPost() && $this->request->isAjax()) {
            $requestType = $this->request->getPost('type');
            $result  = [];
            $objects = $this->modelsManager->createBuilder()->columns([
                'o.id',
                'c.value AS city',
                'a.value AS address',
                'o.house_number',
                'o.flats_count',
                'o.area'
            ])
                ->join('Main\Models\Addresses', 'o.address_id = a.id', 'a')
                ->join('Main\Models\Cities', 'o.city_id = c.id', 'c');
            switch ($requestType) {
                case 'getEmergencyCities':
                    $district_id                = $this->request->getPost('district_id', 'int');
                    $result['e_objects_cities'] = Cities::findByDistrictId($district_id)->toArray();
                    $objects
                        ->from(['o' => 'Main\Models\EmergencyObjects'])
                        ->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();
                    $objects
                        ->from(['o' => 'Main\Models\EmergencyObjects'])
                        ->where('o.city_id = :city_id:', ['city_id' => $city_id]);
                    break;

                case 'getEmergencyObjects':
                    $address_id                  = $this->request->getPost('address_id');
                    $objects
                        ->from(['o' => 'Main\Models\EmergencyObjects'])
                        ->where('o.address_id = :address_id:', ['address_id' => $address_id]);
                    break;

                case 'getTargetCities':
                    $district_id              = $this->request->getPost('district_id', 'int');
                    $result['target_cities']  = Cities::findByDistrictId($district_id)->toArray();
                    $objects
                        ->from(['o' => 'Main\Models\TargetObjects'])
                        ->where('o.district_id = :district_id:', ['district_id' => $district_id]);
                    break;

                case 'getTargetAddresses':
                    $city_id                    = $this->request->getPost('city_id', 'int');
                    $result['target_addresses'] = Addresses::findByCityId($city_id)->toArray();
                    $objects
                        ->from(['o' => 'Main\Models\TargetObjects'])
                        ->where('o.city_id = :city_id:', ['city_id' => $city_id]);
                    break;

                case 'getTargetObjects':
                    $address_id               = $this->request->getPost('address_id');
                    $objects
                        ->from(['o' => 'Main\Models\TargetObjects'])
                        ->where('o.address_id = :address_id:', ['address_id' => $address_id]);
                    break;
            }

            switch (substr($requestType, 0, 4)){
                case 'getT':
                    $result['target_objects'] = $objects->getQuery()->execute()->toArray();
                    break;
                case 'getE':
                    $result['emergency_objects'] = $objects->getQuery()->execute()->toArray();
                    break;
            }
            echo json_encode($result, JSON_NUMERIC_CHECK);
        }