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

时间:2014-11-06 03:41:25

标签: php mysql sql zend-framework zend-framework2

我正在使用Zend Framework 2,并注意我的sql查询在从数据库中提取值以在我的Forms中使用时运行两次。我可以在developpertool中看到它运行了两次(两个不同的运行时间)。它只发生在表单元素上(而不是当我将查询结果回显到表中时)

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

http://samminds.com/2013/03/zendformelementselect-and-database-values/

由于 中号

控制器操作

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),
          );
}

表格

<?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;
    }      

 }

0 个答案:

没有答案