2

我需要以一种形式对多个对象进行水合。这是我使用的:

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

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

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

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

4

1 回答 1

0
  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

需要foreachrows数据设置为实体,将行与表名和生成的地图中的实体进行比较。

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

于 2016-07-20T10:54:04.490 回答