2014-01-07 48 views
2

我安裝了SkeletonApplication並從ZF2 Docs創建了默認相冊模塊。 http://framework.zend.com/manual/2.2/en/user-guide/modules.html從zend框架中的另一個模塊獲取數據2

之後,我創建了類似模塊的專輯模塊。 這是它的領域:category_id和category_name

然後添加額外的專輯表字段:cat_id 正如你明白我想分類專輯。

然後我說這個代碼AlbumForm.php文件字​​段集可供選擇選擇框

$this->add(array(
    'type' => 'Zend\Form\Element\Select', 
    'name' => 'cat_id', 
    'options' => array(
      'value_options' => $selectOptions, 
     'label' => 'Category', 
    ), 
)); 

現在CAT_ID,我怎麼能得到CATEGORY_NAME和CATEGORY_ID從另一個類模塊專輯模塊?

+0

我建議我的博文[Zend \ Form \ Element \ Select and Database Values](http://samminds.com/2013/03/zendformelementselect-and-database-值/) – Sam

回答

2

如果我們能夠看到類別模塊文件的代碼,那將會很好。

首先,你應該考慮如果你真的需要一個新的模塊,因爲它可能應該在同一個模塊中,只是創建新的模型類和新的視圖(如果有的話)。無論如何,在這一點上沒有關係。

如果您使用的是SkeletonApplication我想你的相冊模塊中,在Module.php你有這樣的代碼:

public function getServiceConfig() 
    { 
     return array(
       'factories' => array(
         'Album\Model\AlbumTable' => function($sm) { 
          $tableGateway = $sm->get('AlbumTableGateway'); 
          $table = new AlbumTable($tableGateway); 
          return $table; 
         }, 
         'AlbumTableGateway' => function ($sm) { 
          $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
          $resultSetPrototype = new ResultSet(); 
          $resultSetPrototype->setArrayObjectPrototype(new Album()); 
          return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
         }, 
       ), 
     ); 
    } 

然後可能創建您的分類表類似的東西,像

public function getServiceConfig() 
    { 
     return array(
       'factories' => array(
         'Category\Model\CategoryTable' => function($sm) { 
          $tableGateway = $sm->get('CategoryTableGateway'); 
          $table = new CategoryTable($tableGateway); 
          return $table; 
         }, 
         'CategoryTableGateway' => function ($sm) { 
          $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
          $resultSetPrototype = new ResultSet(); 
          $resultSetPrototype->setArrayObjectPrototype(new Album()); 
          return new TableGateway('category', $dbAdapter, null, $resultSetPrototype); 
         }, 
       ), 
     ); 
    } 

讓我知道它是否有很大的不同。

因爲所有的配置都被合併了,所以無論您是否在其他模塊中,您的控制器中的服務管理器都可以定位Category \ Model \ CategoryTable。

因此,您必須修改您的AlbumForm類,例如在構造函數中可以傳遞依賴關係,例如servicemanager或'Category \ Model \ CategoryTable',因此當您需要檢索$ selectOptions列表時,您可以訪問它來進行任何查詢。

此外,你可以看看samsonasik這樣的更好的解決方案,但它可能更難理解。

並且也您可以檢查此blog post和jamescarr code實現或多或少相同的,你需要的。 這很容易理解,而且是一個很好的解決方案。

+1

就像你說我的相冊模塊的Module.php和類別模塊的Module.php完全一樣。我檢查了samsonasik的鏈接,但像你說的我不明白。我會檢查你的新鏈接並回到這裏。 – ibrahim

+0

如果你沒有得到另一個例子,生病給你提供了更多的代碼,我自己的方法很容易理解 –

+0

http://blog.james-carr.org/2012/09/16/using-select-in-zend-framework- 2 /這是一個理解如何做到這一點的好教程,但它與數據庫沒有關係。所以我沒有得到很好。 :( – ibrahim

2

逸岸,你不必爲你的等級創建一個新的模塊型號 =>把它放在你的相冊模塊:)

它不是多樣信息模塊個好主意......每一個表是很過分:d

正如我看你還是有麻煩我的先例aswer後,我寫了評論和所有的代碼,這樣就可以更好地理解:)

該工廠的ServiceManager是一個概念非常重要非常瞭解!

所以,你首先必須使你的module.php文件出廠配置:

namespace Album; 

use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

use Album\Model\Album; 
use Album\Model\AlbumTable; 
use Album\Model\Category; 
use Album\Model\CategoryTable; 

class Module 
{ 

    public function getServiceConfig() 
    { 
     return array (
      'factories' => array(
       //here already the AlbumTable config 

       // here begin the categoryTable factory 
       'Album\Model\CategoryTable' => function($sm) 
       { 
        //get the tableGateway just below in his own factory 
        $tableGateway = $sm->get('CategoryTableGateway'); 
        //inject the tableGateway in the Table 
        $table = new CategoryTable($tableGateway); 
        return $table; 
       }, 
       //here is the tableGateway Factory for the category 
       'CategoryTableGateway' => function($sm) 
       { 
        //get adapter to donnect dataBase 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        // create a resultSet 
        $resultSetPrototype = new ResultSet(); 
        //define the prototype of the resultSet 
        // => what object will be cloned to get the results 
        $resultSetPrototype->setArrayObjectPrototype(new Category()); 
        //here you define your database table (category) 
        //when you return the tableGateway to the CategoryTable factory 
        return new TableGateway('category', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ), 
    } 
} 

創建模塊/專輯/ src目錄/專輯/型號您Category.php文件:

namespace Album\Model; 

class Category 
{ 
    public $id; 
    public $name; 

    public function exchangeArray(){ 
     $this->id  = (isset($data['id'])) ? $data['id'] : null; 
     $this->name = (isset($data['name'])) ? $data['name'] : null; 
    } 

} 

建立在同一個文件夾中的文件CategoryTable.php(只有使用fetchall()需要選擇輸入法,你只是想所有類別的列表)

namespace Album\Model; 

use Zend\Db\TableGateway\TableGateway; 

use Album\Model\Category; 

class CategoryTable 
{ 
    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) 
    { 
     $this->tableGateway = $tableGateway; 
    } 

    public function fetchAll() 
    { 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 


    public function getCategory($id){ 
     // ... 
    } 

    public function saveCategory(Category $category) 
    { 
     // ... 
    } 

    public function deleteCategory($id) 
    {  
     // ... 
    } 
} 

不要忘記添加在您的AlbumTable :: saveAlbum()方法的新領域CAT_ID;)

創建表單和他的專輯/表夾篩選

namespace Album\Form; 

class AlbumForm{ 
    public function __construct($cat_options){ 

     $this->add(array(
      'name' => 'id', 
      'required' => false, 
      'type' => 'hidden', 
     )); 

     $this->add(array(
      'name' => 'cat_id', 
      'required' => true, 
      'type' => 'Select', 
      'options' => array(
       'value_options' => $cat_options, 
       'label' => 'Category', 
      ) 
      'attributes' => array(
       'value' => 3, // to select a default value 
      ) 
     )); 
     //and the rest of the form 
     $this->add(array(
      'name' => 'submit', 
      'type' => 'Submit', 
      'attributes' => array(
       'id' => 'submitbutton', 
       'value' => 'Go', 
      ), 
     )); 
    } 
} 

注意,我把在與Form相同的命名空間中的特定類中的篩選器。 =>當你在一個模塊中有很多表單和模型時,更容易找到... =>我喜歡這樣,所以我推薦它:D =>您可以爲同一個模型註冊,登錄和resetPassword形式,例如,所有使用相同的用戶類

namespace Album\Form; 

use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\InputFilterAwareInterface; 

class AlbumFilter implements InputFilterAwareInterface 
{ 
    protected $inputFilter; 

    public function setInputFilter(InputFilterInterface $inputFilter) 
    { 
     throw new \Exception("Not used"); 
    } 

    public function getInputFilter() 
    { 
     if(!$this->inputFilter) 
     { 
      $inputFilter = new InputFilter(); 

      $inputFilter->add(array(
       'name'  => 'id', 
       'required' => false, 
       'filters' => array(
        array('name' => 'Int'), 
       ), 
      )); 
      $inputFilter->add(array(
       'name'  => 'cat_id', 
       'required' => true, 
       'filters' => array(
        array('name' => 'Int'), 
       ), 
      )); 

      $inputFilter->add(array(
       'name'  => 'artist', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 45, 
         ), 
        ), 
       ), 
      )); 

      $inputFilter->add(array(
       'name'  => 'title', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 45, 
         ), 
        ), 
       ), 
      )); 
      $this->inputFilter = $inputFilter; 
     } 
     return $this->inputFilter; 
    } 

} 

而現在,一切都被配置,使控制器:)

namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Album\Model\Album; 
use Album\Form\AlbumForm; 

class MyControler extends AbstractActionController 
{ 
    private $CategoryTable; 

    public function getAlbumsTable(){ 
     if(!$this->Album){ 
      // get AlbumTable from the ServiceLocator factory (defined in Module.php) 
      $this->Album = $this->getServiceLocator() 
       ->get('Album\Model\AlbumTable'); 
     } 
    } 

    public function getCategoryTable(){ 
     if(!$this->CategoryTable){ 
      $this->CategoryTable = $this->getServiceLocator() 
       ->get('Album\Model\CategoryTable'); 
     } 
    } 

    public function addAction(){ 
     //get the categories from the database 
     $categories = $this->getCategoryTable()->fetchall(); 

     //make an array from the resultSet 
      // you can do it as well in your controller 
      //or in your form class like I did in my yersterday's answer 
     $cat_options = array(); 
      foreach ($categories as $category) { 
       $cat_options[$category->id]=$category->name; 
      } 

     //create your form with category list as parameter 
     $form = new AlbumForm($cat_options); 
     //here I recomend to create a specific class for you filters 
     // and not put it in your Album class as describe in the zf tutorial 

     // => so you can have several forms using the same Model 
     // and it is more easy to find on the form folder ;) 
     $filter = new AlbumFilter(); 
     //inject InputFilter in your form 
     $form->setInputFilter($filter->getInputFilter()); 
     //inject data in form 
     $form->setData($request->getPost()); 
     //check the form validity (data vs filters) 
     if($form->isValid()){ 
      //create a new album 
      $album = new Album(); 
      //hydrate your Album 
      $album->exchangeArray($form->getData()); 
      //ask the albumsTable to save album in the database 
      $this->albumsTable->saveAlbum($album); 

      //here you can redirect to the Album list 
      $this->redirect()->toRoute('Album'); 
     } 
     //call the viewModel if first time you see the page 
     // or if validation errors of the form. 
     return array('form' =>$form); 
    } 
} 

然後你只需要讓你的看法...我不會在這裏把這個代碼放進去;)

如果您仍然需要在應用程序中創建多個模塊,則可以使用工廠在getobjectTable()方法中調用控制器中的任何objectTable:如Carlos所說,getServiceConfig()數組全部合併爲一個,所以你可以從應用程序的任何地方得到任何表格:)

+0

我做了你所說的一切,但添加專輯頁面出現此錯誤:致命錯誤:調用成員函數fetchall()在C:\ xampp \ htdocs \ tasarim \ module \ Album \ src \ Album \ Controller \ AlbumController.php在第39行 – ibrahim