水合多个物体zf2

时间:2016-07-17 20:46:01

标签: forms zend-framework2 fieldset hydration

我需要在一种形式中修改多个对象。这是我使用的:

  • 产品表格 - 我有一个表格,我称之为三个字段集
  • 产品字段集
  • 促销字段集
  • 类别字段集

我有所有必要表格的模型,以下是产品型号的示例:

int i, arrLen = arr.length;
 StringBuilder tmp = new StringBuilder();
 for (i=0; i<arrLen-1; i++)
    tmp.append(arr[i] +" - ");
 tmp.append(arr[arrLen-1]);

 System.out.println( tmp.toString() );

在我的控制器中,我想将数据绑定到我的视图,以便我可以编辑它们。

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

我需要对结果进行水合并返回一个对象,我将在控制器中使用该对象来绑定表单。

1 个答案:

答案 0 :(得分:0)

  1. 您可以手动填充数据库中的实体。

  2. 如果要自动填充需要在数据库和实体之间创建地图。我创建了一个用于在数据库和实体之间创建地图的库,使用实体https://github.com/newage/annotations中的注释。

  3. 下一步。

    从表中获取不同的数据时。例如:

    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
    

    需要按foreach执行rows并将数据设置为实体,比较行与表名和生成的地图中的实体。

      

    从DB自动生成实体树是我的下一个项目。   但它没有完成。 https://github.com/newage/zf2-simple-orm

相关问题