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.

Cannot access empty property

Cannot access empty property in /home/bender/projects/engine/app/modules/admin/controllers/AdminController.php on line 86: Here is the method of controller:

    private function getHotelsCountPerSupplier()
    {

        $propertySupplier = new Property\PropertySupplier();
        $hotelsComplexResultSet = $propertySupplier->getHotelsCountPerSupplier();

        $count = $hotelsComplexResultSet->count();

        for ($i = 0; $i < $count; $i++) {
            $data[] = $hotelsComplexResultSet[$i]->toArray();
        }

        return $data;
    }

And the problem line is

    $data[] = $hotelsComplexResultSet[$i]->toArray();

on the 4 iteration. hotelsComplexResultSet is a Resultset\Complex with count 10.

And here is the the model method getHotelCountPerSupplier() :

    public function getHotelsCountPerSupplier()
    {        
        $query = $this->getDI()->getModelsManager()->createQuery("SELECT * FROM \Modules\Admin\Models\Property\PropertySupplier as ps
        JOIN \Modules\Admin\Models\Supplier as s ON ps.supplier_id = s.id GROUP BY s.id");

        return $query->execute();
    }

It looks like a bug



6.3k

As far as i remember you can't access an index on a resultset like an array $hotelsComplexResultSet[$i]. You would have to convert the resultset to an array or use the internal functions. On the ResultSet class you have the function offsetGet(unknown $property) it works pretty much like the [$i] from arrays

Try to do it like this :

    $data[] = $hotelsComplexResultSet->offsetGet($i)->toArray();


14.8k
edited Nov '14

The problem is the same Browser says: No data received

private function getHotelsCountPerSupplier()
{

    $propertySupplier = new Property\PropertySupplier();
    $hotelsComplexResultSet = $propertySupplier->getHotelsCountPerSupplier()->setHydrateMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);



    $count = $hotelsComplexResultSet->count();

    for ($i = 0; $i < $count; $i++) {

        $data[] = $hotelsComplexResultSet->offsetGet($i);
        //$data[] = $hotelsComplexResultSet->offsetGet($i)->toArray();
    }
    var_dump($data);exit;
    return $data;
}

But if i dump after first iteration i get that i want go get:

for ($i = 0; $i < $count; $i++) {
        var_dump($hotelsComplexResultSet->offsetGet($i));
        exit;            
    }


6.3k
Accepted
answer

If you browser is not receiving any data, then the problem is with the Controller/Object/Whatever is receiving the returned $data.

Tell me if this works :

private function getHotelsCountPerSupplier()
{
  $propertySupplier = new Property\PropertySupplier();
  $hotelsComplexResultSet = $propertySupplier->getHotelsCountPerSupplier();

  $count = $hotelsComplexResultSet->count();

  for ($i = 0; $i < $count; $i++) {
    $data[] = $hotelsComplexResultSet->offsetGet($i)->toArray();
  }

  var_dump($data);
  die();

  return $data;
}


14.8k

Actually yes, works :)