Cakephp 3.3: how to set up my looped data in view template to save multiple records in controller


#1

Model:
public function initialize(array $config)
{
parent::initialize($config);

    $this->table('questions_respondents');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Questions', [
        'foreignKey' => 'question_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Respondents', [
        'foreignKey' => 'respondent_id',
        'joinType' => 'INNER'
    ]);
}

Controller:
if ($this->request->is(‘post’)) {
$table = TableRegistry::get(‘QuestionsRespondents’);
$entities = $table->newEntities($this->request->data());
foreach ( $entities as $entity) {
$data[] = [

                'answer' => $entity['answer'],
                'respondent_id' => $entity['id'],
                'question_id' => 1
               
            ];
           }
          $ents = $table->newEntities($this->request->data());
          
         if ($this->QuestionsRespondents->saveMany($ents)) {
            $this->Flash->success(__('The questions respondent has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The questions respondent could not be saved. Please, try again.'));
    }

View

<?php foreach ($prompts as $quesresp): ?>
                <?= h($quesresp->qtext) ?>
                <!--echo question_id within an hidden field -->
                <?php echo $this->Form->input('Answer:',['type'=>'text']); ?> 
                <?php echo $this->Form->input('respondent_id', ['type' => 'hidden', 'value' => 1]);?>   
             <?php endforeach; ?>

right now, nothing is saving. How do i setup my code to prepare the data in the view template in order to perform a multiple save on form submit?