Hello everyone, i just trying to find a way to project my business logic
with associated tables (models). Here i have a table that store my products
and another table in charge of control the items availability. I need this
table itemAvailability because i have business units in my app, so is
necessary to filter stocks qty for units.
Please check my following layers and tell me your opinion if something could
be better.
THE MAPPER
class Default_Model_ItemMapper
{
public function fetchAll()
{
$select=$this->getDbTable()->select()
->setIntegrityCheck(false)
//->from(array('s'=>'searchItems',array('item__id')))
->from(array('i'=>'item'))
->join(array('ia'=>'itemAvailability'),
'i.item__id=ia.item__id__FK'))
->where('ia.businessUnit__code__FK=?',//here by business unit code
(FK))
->order(array('i.item__fullName'));
$this->_paginator = new Zend_Paginator(new
Zend_Paginator_Adapter_DbTableSelect($select));
$this->_paginator->setCurrentPageNumber(Zend_Controller_Front::getInstance()->getRequest()->getParam('page'));
$this->_paginator->setItemCountPerPage(30);
foreach ($this->_paginator as $item__ROW)
{
$itemAvailability__MDL=new Default_Model_ItemAvailability();
$itemAvailability__MDL->setStockQty($item__ROW->itemAvailability__stockQty)//here
is my joined field feeding the ItemAvailability instance
;
$item__MDL = new Default_Model_Item();
$item__MDL->setId($item__ROW->item__id)
->setName($item__ROW->item__name)
->setFullName($item__ROW->item__fullName)
->setDescription($item__ROW->item__description)
->setGrossWeight($item__ROW->item__grossWeight)
->setItemAvailability__MDL($itemAvailability__MDL)// here is my
instance (OOP/association) of Default_Model_ItemAvailability
;
$item__RWSIST[$item__ROW->item__id]=$item__MDL;
}
return $item__RWSIST;
}
public function getPaginator()
{
return $this->_paginator;
}
}
THE MODEL
class Default_Model_Item extends Default_Model_Abstract
{
protected $_item__id;
protected $_item__name;
protected $_item__fullName;
protected $_item__description;
protected $_item__grossWeight;
protected $_item__tagHighlight;
protected $_itemAvailability__MDL;
//here __set and __get methods of Item instance
// here specially the method for setting and get my ItemAvailability
instance
public function setItemAvailability__MDL($value)
{
$this->$_itemAvailability__MDL = $value;
return $this;
}
public function getItemAvailability__MDL()
{
return $this->$_itemAvailability__MDL;
}
public function isAvailable()
{
//here i do some logic with the self Item instance and also with
the associatied ItemAvailability instance like:
if($this->$_itemAvailability__MDL->getStockty > 0)
//return ok(true) available for sale
}
}
public function getPaginator()
{
return $this->getMapper()->getPaginator();
}
}
THE CONTROLLER
And the in my controller i retrieve my items doing this:
class ItemController extends Zend_Controller_Action{
public function listAction()
{
$this->view->item__RWSIST=$this->_item__MDL->fetchAll();//HERE I
PASS the array with the instances for the view
$this->view->item__RWSPGT=$this->_item__MDL->getPaginator(); here i
pass the paginator
}
}
THE VIEW
Finally in my view (list.phtml i list the items:
<?echo
$this->paginationControl($this->item__RWSPGT,'Sliding','paginator_item.phtml');
?>
<?foreach ($this->item__RWSPGT as $item__ROW):?>
//here i can use the logics of the Default_Model_Item
<?if($this->item__RWSIST[$item__ROW->item_id]->isAvailable()):?>//here is my
array with each one instance available for each item
here i show the buy button for example.
<?endif?>
<?endforeach;?>
<?echo
$this->paginationControl($this->item__RWSPGT,'Sliding','paginator_item.phtml');
?>
What do you guys think of this approach? My domain model logic looks fine
for you? Or maybe i should use a service layer?
I just met Fowler's patterns and still trying to realize how to deal with
such patterns.I think in terms of OPP i can do this Classes/Objects
association but i dont know if im breaking any rule when my model is based
on Fowler's Domain Model.
Looking forward for comments please.
Thanks,
Eduardo