2

I need to hyrdate multiple objests in one form. Here is what I use:

  • Product Form - I have a form where I call three fieldsets
  • Product Fieldset
  • Promotion Fieldset
  • Category Fieldset

I have Models for all the necessary tables, here is an example for the product model:

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

In my controllers I want to bind the data to my view so I can edit them.

try {
        $aProduct = $this->productService->findProduct($iId);
      } catch (\Exception $ex) {
        // ...
    }

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

In the first place I need to select all the necessary information from the DB. I join three tables product, promotion and category tables. I must return the data to my controller as objects and bind them in my form to be able to edit on the view page.

Please give me some ideas how to accomplish this so I can continue with my development. I am stuck.

I will appreciate all the links which can help me or give me any ideas/examples from the real life.

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

I need to hydrate the result and return an object which I will use in the controller to bind the form.

Sezgin
  • 43
  • 4
  • Maybe this link will help you. https://framework.zend.com/manual/2.4/en/modules/zend.form.collections.html – newage Jul 18 '16 at 10:17
  • Thank you for the link @newage but I need to hydrate the result in the mapper where I select the data from the DB. I join 3 tables and I have to distinguish these three tables in the model somehow which I am not sure how to do. I will try to add some more example for that. Thank you again. – Sezgin Jul 18 '16 at 18:47
  • If your project is new, I recommend you to use Doctrine. If you want to use Zend\Db with join you need parse all data from DB and fill entities manually. I have like functionality. If you interesting I can describe details about my realization. – newage Jul 19 '16 at 09:18
  • @newage Thank you for your advice. Obviously, I will need to go with your recommendation cause I cannot find other solution at the moment. (: – Sezgin Jul 19 '16 at 19:49

1 Answers1

0
  1. You can to fill entities from a database manually.

  2. If you want to fill automatically need to create a map between a database and entities. I made a library for making a map between DB and entities use annotations in entities https://github.com/newage/annotations.

Next step.

When you get different data from tables. Example:

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

Need do foreach by rows and set data to entities comparing row with table name and Entity from a generated map.

Auto generating tree of entities from DB is my next project. But it's do not finished. https://github.com/newage/zf2-simple-orm.

newage
  • 899
  • 7
  • 18