Forms 多目标zf2

Forms 多目标zf2,forms,zend-framework2,fieldset,hydration,Forms,Zend Framework2,Fieldset,Hydration,我需要以一种形式混合多个对象。以下是我使用的: 产品表单-我有一个表单,我称之为三个字段集 产品字段集 提升字段集 类别字段集 我有所有必要表格的模型,以下是产品模型的示例: class Product implements ProductInterface { /** * @var int */ protected $Id; /** * @var string */ protected $Title; /** * @

我需要以一种形式混合多个对象。以下是我使用的:

  • 产品表单-我有一个表单,我称之为三个字段集
  • 产品字段集
  • 提升字段集
  • 类别字段集
我有所有必要表格的模型,以下是产品模型的示例:

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);
首先,我需要从数据库中选择所有必要的信息。我加入了三个表:产品表、促销表和类别表。我必须将数据作为对象返回到我的控制器,并将它们绑定到我的表单中,以便能够在查看页面上进行编辑

请给我一些想法如何实现这一点,这样我可以继续我的发展。我被卡住了

我将感谢所有能帮助我或给我任何来自现实生活的想法/例子的链接

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");
}
我需要生成结果并返回一个对象,我将在控制器中使用该对象绑定表单

  • 您可以手动从数据库中填充实体

  • 如果要自动填充,需要在数据库和实体之间创建映射。我制作了一个库,用于在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
    
    需要通过
    rows
    执行
    foreach
    ,并将数据设置为实体,将行与表名和生成的映射中的实体进行比较

    从数据库自动生成实体树是我的下一个项目。 但是它还没有完成


    也许这个链接会对你有所帮助。谢谢@newage的链接,但我需要在映射器中添加结果,在映射器中我从数据库中选择数据。我连接了3个表,我必须在模型中以某种方式区分这三个表,我不知道该怎么做。我将尝试添加更多的例子。再次感谢你。如果你的项目是新的,我建议你使用条令。如果要将Zend\Db与join一起使用,则需要手动解析Db中的所有数据并填充实体。我喜欢功能。如果您感兴趣,我可以详细描述我的认识。@newage谢谢您的建议。显然,我需要接受你的建议,因为我目前找不到其他解决方案。(: