2016-07-17 3 views
2

複数のオブジェクトを1つのフォームにhyrdateする必要があります。 - 私はすべてのための3つのフィールドセット 複数のオブジェクトを水和させるzf2

  • 製品フィールドセット
  • 推進フィールドセット
  • カテゴリーフィールドセット
  • は私が持っているモデルを呼び出すフォームを持っている

    • 製品形態を:ここで私が使用するものです必要なテーブルは、製品モデルの例です:

      class Product implements ProductInterface 
      { 
          /** 
          * @var int 
          */ 
          protected $Id; 
      
          /** 
          * @var string 
          */ 
          protected $Title; 
      
          /** 
          * @var float 
          */ 
          protected $Price; 
      
          /** 
          * @var string 
          */ 
          protected $Description; 
      
          /** 
          * @var string 
          */ 
          protected $Url; 
      
          /** 
          * @var \DateTime 
          */ 
          protected $DateAdded; 
      
          /** 
          * @var string 
          */ 
          protected $Image; 
      
          /** 
          * @var int 
          */ 
          protected $Status; 
      
          /** 
          * @return int 
          */ 
          public function getId() 
          { 
           return $this->Id; 
          } 
      
          /** 
          * @param int $Id 
          */ 
          public function setId($Id) 
          { 
           $this->Id = $Id; 
          } 
      
          /** 
          * @return string 
          */ 
          public function getTitle() 
          { 
           return $this->Title; 
          } 
      
          /** 
          * @param string $Title 
          */ 
          public function setTitle($Title) 
          { 
           $this->Title = $Title; 
          } 
      
          /** 
          * @return float 
          */ 
          public function getPrice() 
          { 
           return $this->Price; 
          } 
      
          /** 
          * @param float $Price 
          */ 
          public function setPrice($Price) 
          { 
           $this->Price = $Price; 
          } 
      
          /** 
          * @return string 
          */ 
          public function getDescription() 
          { 
           return $this->Description; 
          } 
      
          /** 
          * @param string $Description 
          */ 
          public function setDescription($Description) 
          { 
           $this->Description = $Description; 
          } 
      
          /** 
          * @return string 
          */ 
          public function getUrl() 
          { 
           return $this->Url; 
          } 
      
          /** 
          * @param string $Url 
          */ 
          public function setUrl($Url) 
          { 
           $this->Url = $Url; 
          } 
      
          /** 
          * @return \DateTime 
          */ 
          public function getDateAdded() 
          { 
           return $this->DateAdded; 
          } 
      
          /** 
          * @param \DateTime $DateAdded 
          */ 
          public function setDateAdded($DateAdded) 
          { 
           $this->DateAdded = $DateAdded; 
          } 
      
          /** 
          * @return string 
          */ 
          public function getImage() 
          { 
           return $this->Image; 
          } 
      
          /** 
          * @param string $Image 
          */ 
          public function setImage($Image) 
          { 
           $this->Image = $Image; 
          } 
      
          /** 
          * @return int 
          */ 
          public function getStatus() 
          { 
           return $this->Status; 
          } 
      
          /** 
          * @param int $Status 
          */ 
          public function setStatus($Status) 
          { 
           $this->Status = $Status; 
          } 
      

      私のコントローラではデータを私のビューにバインドして編集できるようにしたいと思います。

      try { 
           $aProduct = $this->productService->findProduct($iId); 
           } catch (\Exception $ex) { 
           // ... 
          } 
      
      $form = new ProductForm(); 
      $form->bind($aProduct); 
      

      まず、DBから必要な情報をすべて選択する必要があります。私は3つのテーブル製品、プロモーション、カテゴリーテーブルに参加します。ビューのページで編集できるように、データをオブジェクトとしてコントローラに返し、フォームにバインドする必要があります。

      これを達成する方法をいくつか教えてください。開発を続けることができます。ハマった。

      実際の生活から私を助けたり、アイデアを与えたりすることができます。

      public function findProduct($Id) 
      { 
          $iId = (int) $Id; 
      
          $sql = new Sql($this->dbAdapter); 
          $select = $sql->select('product'); 
          $select->join('promotion', 'promotion.ProductId = product.Id', array('Discount', 'StartDate', 'EndDate', 'PromotionDescription' => 'Description', 'PromotionStatus', 'Type'), 'left'); 
          $select->join('producttocategory', 'producttocategory.ProductId = product.Id', array('CategoryId'), 'left'); 
          $select->join('category', 'category.Id = producttocategory.CategoryId', array('ParentId', 'Title', 'Description', 'Url', 'DateAdded', 'Image', 'Status'), 'left'); 
      
          $where = new Where(); 
          $where->equalTo('product.Id', $iId); 
          $select->where($where); 
      
          $stmt = $sql->prepareStatementForSqlObject($select); 
          $result = $stmt->execute(); 
      
          if ($result instanceof ResultInterface && $result->isQueryResult()) { 
           $resultSet = new HydratingResultSet($this->hydrator, $this->productPrototype); 
      
           return $resultSet->initialize($result); 
          } 
      
          throw new \Exception("Could not find row $Id"); 
      } 
      

      私は結果を水和し、コントローラでフォームをバインドするために使用するオブジェクトを返す必要があります。

    +0

    多分、このリンクはあなたを助けるでしょう。 https://framework.zend.com/manual/2.4/en/modules/zend.form.collections.html – newage

    +0

    リンク@newageをありがとうございますが、私はマッパーで結果を水和する必要があります。 DB。私は3つのテーブルに参加し、モデルのこれらの3つのテーブルを何らかの形で区別する必要がありますが、その方法はわかりません。私はそれのためのいくつかの例を追加しようとします。ありがとうございました。 – Sezgin

    +0

    プロジェクトが新規の場合は、Doctrineを使用することをお勧めします。結合でZend \ Dbを使用する場合は、DBからすべてのデータを解析し、エンティティを手動で入力する必要があります。私は同じような機能を持っています。興味深い場合は、私の実現についての詳細を記述することができます。 – newage

    答えて

    0
    1. データベースからエンティティを手動で入力できます。

    2. 自動的に塗りつぶしたい場合は、データベースとエンティティの間にマップを作成する必要があります。私は、エンティティhttps://github.com/newage/annotationsでアノテーションを使用して、DBとエンティティ間のマップを作成するためのライブラリを作成しました。

    次のステップ。

    テーブルから異なるデータを取得する場合。例:

    SELECT 
    table1.id AS table1.id, 
    table1.title AS table1.title, 
    table2.id AS table2.id, 
    table2.alias AS table2.alias 
    FROM table1 
    JOIN table2 ON table1.id = table2.id 
    

    rowsforeachを行うと、生成されたマップからテーブル名とエンティティとの行を比較するエンティティにデータを設定する必要があります。

    DBからのエンティティの自動生成ツリーは私の次のプロジェクトです。 しかし、それは完了していません。 https://github.com/newage/zf2-simple-orm

    関連する問題