We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



11.2k

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();


25.0k
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;            
    }


11.2k
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;
}


25.0k

Actually yes, works :)