水合物多个物体zf2



我需要在一个表单中hyrdate多个对象

  • 产品表单-我有一个表单,其中我调用了三个字段集
  • 产品字段集
  • 促销字段集
  • 类别字段集

我有所有必要表格的模型,这里有一个产品模型的例子:

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

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

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

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

下一步。

当您从表中获取不同的数据时。示例:

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,并将数据设置为比较行和表名的实体以及生成的映射中的实体。

从数据库中自动生成实体树是我的下一个项目。但它还没有结束。https://github.com/newage/zf2-simple-orm.

最新更新