2013-03-21 68 views
3

我正在學習zf2,我正面臨涉及2(最終更多)模塊一起工作的問題。請注意,我仔細閱讀this post(以及相關的),這對我有很大的幫助。我將解釋一下這個問題:zf2形式:填充選擇字段與來自數據庫的數據

  • 使用第一個模塊(FrOption),管理員可以管理網站表單選項。所有選項都存儲在一個數據庫表是這樣的:

ID | FIELD_NAME | FIELD_VALUE
1 |國內|德國|
2 |國家|法國|
3 |性別|男|
4 |性別|女性|
5 | tipo | Car |
6 | tipo | Fly |
...

  • 在我的模塊(FrItem)我已經建立了它需要一些 「FIELD_NAME」 字段的表格。 我的「item」表格如下:

id | name | id_tipo |
1 | Fiat | 5 |
2 |漢莎航空| 6 |
3 | Ford | 5 |
4 |法國航空6 |
...

(id_tipo是一個選項FK)

還認爲:

  • 我的實體具有 「TIPO」 屬性,二傳手+吸氣
  • 我已經建立了一個ItemHydrator到「地圖」id_tipo db字段爲「tipo」實體屬性
  • 作爲一項測試,我已經在我的表單類中添加了這個字段,並且在視圖和編輯模式下一切正常:

    $this->add(
    'type' => 'Zend\Form\Element\Select', 
    'name' => 'id_tipo', 
    'options' => array (
        'label' => 'Tipo', 
        'empty_option' => 'Select', 
        'value_options' => array ('5' => 'Car', '6' => 'Fly')) 
    

    );

現在我想「鏈接」兩個模塊:value_options應該從FrOption來,所以我在尋找實現這一要求的最好方式動態數組。

我認爲一個解決辦法是這樣的:

  1. 添加到我的FrOption/src目錄/ FrOption /服務/ FrOption.php服務類getOptionByName($字段名)方法
  2. 在FrItem /模塊.php檢索服務,然後使用getOptionByName獲取數據,最後將所有注入到Form中。

這可能是一個明智的和可行的解決方案嗎?在性能(選項表可能增長)方面,你還有什麼關係? 如果有的話,你用什麼樣的解決方案來解決類似的問題?

感謝

+0

的可能的複製 - > http://stackoverflow.com/ question/12460840/create-a-drop-down-list-in-zend-framework-2 – Crisp 2013-03-21 15:16:45

+0

可能的重複 - > http://stackoverflow.com/questions/15527934/zend-framework-2-0-fill-option -from-database – Sam 2013-03-21 15:18:37

+0

@IamFraz如果你沒有找到答案,請參考我的BlogPost但:http://samminds.com/2013/03/zendformelementselect-and-database-values/ – Sam 2013-03-22 16:18:19

回答

9

這可以很容易地通過遵循以下兩個步驟中的Zend Framework2步驟1.

加上適配器的情況下做什麼實例化窗體類的 控制器動作

$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); 
$form = new TestForm ($dbAdapter); 

步驟2在您的形式

namespace Test\Form; 

use Zend\Form\Form; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\Adapter\Adapter; 
class TestForm extends Form 
{ 
    protected $adapter; 
    public function __construct(AdapterInterface $dbAdapter) 
    { 
     $this->adapter =$dbAdapter; 
       parent::__construct("Test Form"); 
     $this->setAttribute('method', 'post'); 
       //your select field 
$this->add(array(
       'type' => 'Zend\Form\Element\Select', 
       'name' => 'name', 
       'tabindex' =>2, 
       'options' => array(
         'label' => 'Author', 
         'empty_option' => 'Please select an author', 
         'value_options' => $this->getOptionsForSelect(), 
       ) 
     )); 

       // another fields 

} 
     public function getOptionsForSelect() 
    { 
     $dbAdapter = $this->adapter; 
     $sql  = 'SELECT id,name FROM newsauthor where active=1 ORDER BY sortorder ASC'; 
     $statement = $dbAdapter->query($sql); 
     $result = $statement->execute(); 

     $selectData = array(); 

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

} 

在這裏你去,你準備好rock.I希望它可以幫助你

+0

ZF3會怎麼做? – 2017-05-24 11:39:28

+0

嗨Keyz,我需要看看它。我認爲你可以遵循同樣的方法。 https://docs.zendframework.com/zend-form/quick-start/。 – 2017-05-25 19:02:51

-1
Try: 
// add code on controller 
$arrTipoData = array(); 
$tipoResults = array('5' => 'Car', '6' => 'Fly',); // this part change your database value 
foreach ($tipResults as $key => $val) { 
$arrTipoData[$key] = $va; 
} 

$arrGenderData = array(); 
$genderResults = array('3' => 'Male', '4' => 'Female',); // this part change your database value 
foreach ($genderResults as $key => $val) { 
$arrGenderData[$key] = $va; 
} 
$dataParams['idTipo'] = $arrTipoData; 
$dataParams['gender'] = $arrGenderData; 
$form = new UserForm($dataParams); 

<?php 
namespace Register\Form; 
use Zend\Form\Form; 
use Zend\Form\Element; 


class UserForm extends Form 
{ 
    protected $portalTable; 

    public function __construct($params = array()) 
    { $name = isset($params['name'])?$params['name']:''; 
     parent::__construct('user'); 
     $this->setAttribute('method', 'post'); 
     $this->setAttribute('enctype', 'multipart/form-data'); 

    $idTipo = (isset($params['idTipo']) && count($params['idTipo']) > 0)?$params['idTipo']:array(); 
     $this->add(array(
       'type' => 'Select', 
       'name' => 'id_tipo', 
       'options' => array(
         'label' => 'Tipo', 
         'empty_option' => 'Select Tipo', 
         'value_options' => $idTipo, 

       ) 
     )); 

     $gender = (isset($params['gender']) && count($params['gender']) > 0)?$params['gender']:array(); 
     $this->add(array(
       'type' => 'Select', 
       'name' => 'gender_id', 
       'options' => array(
         'label' => 'Gender', 
         'empty_option' => 'Select', 
         'value_options' => $gender, 

       ) 
     )); 

    } 
} 
相關問題