首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >水合物多目标zf2

水合物多目标zf2
EN

Stack Overflow用户
提问于 2016-07-17 20:46:00
回答 1查看 273关注 0票数 2

我需要用一种形式对多个讣告进行处理。以下是我所用的:

  • 乘积表-我有一个表单,其中我调用三个字段集。
  • 积场集
  • 晋升场
  • 范畴场集

我有所有必需表的模型,下面是产品模型的一个示例:

代码语言:javascript
运行
复制
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;
   }

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

代码语言:javascript
运行
复制
try {
        $aProduct = $this->productService->findProduct($iId);
      } catch (\Exception $ex) {
        // ...
    }

$form = new ProductForm();
$form->bind($aProduct);

首先,我需要从DB中选择所有必要的信息。我加入了三个表格--产品表、促销表和类别表。我必须将数据作为对象返回给我的控制器,并以我的形式绑定它们,以便能够在视图页面上进行编辑。

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

我将感谢所有的链接,可以帮助我或给我任何想法/例子,从现实生活。

代码语言:javascript
运行
复制
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");
}

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

EN

回答 1

Stack Overflow用户

发布于 2016-07-20 10:54:04

  1. 您可以手动填充数据库中的实体。
  2. 如果要自动填充,需要在数据库和实体之间创建映射。我创建了一个库,用于在实体https://github.com/newage/annotations中使用注释在DB和实体之间建立映射。

下一步。

当您从表中得到不同的数据时。示例:

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38425789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档