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.