0

I'm currently working on a Zend 2 project and I have some problems with a foreach within another foreach. Here my code :

foreach($test as $one){
    echo $one->name;

    foreach($place as $param){
        echo $param['name_place'];
    }
    unset($param);
}

I get the following error :

Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\RuntimeException' with message 
'This result is a forward only result set, calling rewind() after moving forward is not supported' in 
/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Result.php:154 
Stack trace: #0 /module/Check/view/check/check/choix-niveau.phtml(25): 
Zend\Db\Adapter\Driver\Pdo\Result->rewind() #1 /vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(506): 
include('/var/www/vhosts...') #2 /vendor/zendframework/zendframework/library/Zend/View/View.php(205):
Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel)) 
#3 /vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(103):
Zend\View\View->render(Object(Zend\View\Model\ViewModel)) #4 [internal function]:
Zend\Mvc\View\Http\DefaultRenderingStrategy->render( in /vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Result.php on line 154

And the models from the 2 foreachs :

public function getAllPlace()
{
    $select = new Select();
    $select->from('place')
            ->join('level', 'level.id = place.id_level', array('nom'))
            ->order('level.id');

    $statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($select);
    $resultSet = $statement->execute();
    $resultSet->buffer();
    return $resultSet;
}

And the second model

public function getAllTry()
{
    $select = new Select();
    $select->from('try')
    ->order('id');

    $resultSet = $this->tableGateway->selectwith($select);
    $resultSet->buffer();
    return $resultSet;
}

The problem is coming from $place as $parambecause if the code is $place as $place there is no error. But I need to change the name variable to unset him.

Thanks per advance!

PokeRwOw

EDIT :

In the array $test there are 3 values and in the array $place there are 2 values. So the result after launch :

- First value of $test
  - First value of $place
  - Second value of $place
-Second value of $test

And this is here where It's finish. So certainly It is coming for the cursor of the second foreach or something like this ?

If I make the two foreach separately and not imbrique Its working...

PokeRwOw
  • 609
  • 1
  • 6
  • 29

2 Answers2

0

Since we don't have the full code nor we know the datatype of $test and $place I assume that you are missing ->current() or ->toArray()` somewhere.

Show us the content of $test and $place, please.

    foreach($test as $key => $one){
        echo $one->name;
            foreach($place[$key] as $param){
                echo $param['name_place'];
            }
            unset($param);
    }
Stanimir Dimitrov
  • 1,872
  • 2
  • 20
  • 25
  • I'm not sure there is a tie-in with my problem, check edited question – PokeRwOw Aug 24 '15 at 14:02
  • Check out the code now. Also I meant that I need to see the contents of var_dump(); in order to determinate what's exactly wrong, since I also had this problem many times, but the cases could be different. – Stanimir Dimitrov Aug 24 '15 at 18:17
0

$place is a forward only resultset. The first loop takes it to the end. So you cannot use it in a for loop again since it is already in the end.

Pradeep
  • 2,469
  • 1
  • 18
  • 27