Php 为什么我的SQL查询运行了两次?

Php 为什么我的SQL查询运行了两次?,php,mysql,sql,zend-framework,zend-framework2,Php,Mysql,Sql,Zend Framework,Zend Framework2,我使用的是Zend Framework 2,注意到当从数据库中提取要在表单中使用的值时,我的sql查询运行了两次。我可以在developertool中看到,它运行了两次两种不同的运行时间。它只在表单元素中发生,而不是在我将查询结果回送到表中时发生 我使用以下链接作为指导: 谢谢 M 控制器动作 public function indexAction() //Display the pro greetings page { $this->layout()->setVariabl

我使用的是Zend Framework 2,注意到当从数据库中提取要在表单中使用的值时,我的sql查询运行了两次。我可以在developertool中看到,它运行了两次两种不同的运行时间。它只在表单元素中发生,而不是在我将查询结果回送到表中时发生

我使用以下链接作为指导:

谢谢 M

控制器动作

public function indexAction() //Display the pro greetings page
{
    $this->layout()->setVariable('menu', 'verified');
    $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');         
    $form = new ProLicenceForm($dbAdapter); 
    $form->get('submit')->setValue('Submit');
    $pro_id='22';
    $proBackEnd = new ProSide($dbAdapter);      
    $form->get('pro_id')->setValue($pro_id);   
   // $user_id=$this->zfcUserAuthentication()->getIdentity()->getId(); //causes error if not register need to block or add logic to redirect
   // $form->get('user_id')->setValue($user_id); //pass the user id from the user module 
     $request = $this->getRequest();      
     if ($request->isPost()) {
         $proLicence = new ProLicence();                 
         $form->setInputFilter($proLicence->getInputFilter());  
         $form->setData($request->getPost());   
         if ($form->isValid()) {
             $proLicence->exchangeArray($form->getData());        
             $this->getProLicenceTable()->saveProLicence($proLicence);                                   
           //  return $this->redirect()->toRoute('pro', array('action'=>'proVerification'));  //redirect to next step                
         }
          else {
              echo "ERROR HOMMIE";
          }           
    }
      return array(
          'form' => $form,
          'proLicences' => $proBackEnd->getProSideLicence($pro_id),
          );
}
形式


为什么不使用zend select查询获取结果?感谢您的回复。你能发个链接吗?我不熟悉Zend Select查询。我正在使用此方法获得正确的结果我只是让它们运行了两次。我认为这对您很有用我不希望使用TableGateway方法,因为我正在使用联接和其他自定义查询功能。需要SaveProLience代码。
<?php
namespace Pro\Form;

use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;

 class ProLicenceForm extends Form
 {
     public function __construct(AdapterInterface $dbAdapter)
     {
        // $this->setDbAdapter($dbAdapter);
          $this->adapter =$dbAdapter;
         // we want to ignore the name passed
         parent::__construct('pro-licence-form');

         $this->add(array(
             'name' => 'pro_id',
             'type' => 'Hidden',
         ));
         $this->add(array(
             'name' => 'licence_id',
             'type' => 'Hidden',
             'options' => array(
             ),
             'attributes' => array( 
                  //'required' => 'required'
                  )
         )); 
         $this->add(array(
             'name' => 'licence_name_id',
            'type' => 'Zend\Form\Element\Select',
             'options' => array(
                 'label' => 'Licence',
                 'value_options' => $this->getLicenceOptions(),
                 'empty_option' => '--- please choose ---'

             ),
             'attributes' => array( 
                 'placeholder' => 'Choose all that apply'             
                 )
         )); 
         $this->add(array(
             'name' => 'licence_number',
            'type' => 'Text',
             'options' => array(
                 'label' => 'Licence Number'
                 )

         )); 
         $this->add(array(
             'name' => 'submit',
             'type' => 'Submit',
             'options' => array(
             'label'=> 'Primary Button',
                 ),
             'attributes' => array(
                 'value' => 'Go',
                 'id' => 'submitbutton',
             ),
         ));

     }

      /*
      * SQL statements used to bring in optiosn
      */


     public function getLicenceOptions()
    {
        $dbAdapter = $this->adapter;
        $sql       = 'SELECT licence_name_id,licence_name  FROM licence_name_table ORDER BY licence_name';
        $statement = $dbAdapter->query($sql);
        $result    = $statement->execute();

        $selectData = array();

        foreach ($result as $res) {
            $selectData[$res['licence_name_id']] = $res['licence_name'];
        }
        return $selectData;
    }      

 }