Welcome Guest, Not a member yet? Register   Sign In
At what point do you seperate things into different models?
#15

(This post was last modified: 06-22-2016, 05:15 AM by sintakonte.)

i would leave this in separate Models
If i understand you correctly a possible approach could be, to work with collections - below you see an example (i simple wrote it in notepad down now - so there could be some typos - but you should be able to see the point)
Range_Model
PHP Code:
class Range_Model extends CI_Model
{

    public function 
__construct()
    {
        
$this->load->model("Product_Model");
    }
    
    public function 
getItems()
    {
        
//your query to get the ranges
        
$objCollection = new Range_Collection($query->result("Range_Object"));
        
$objCollection->addProductCollection($this->Product_Model->getItems());
        return 
$objCollection;
    }

Product Model
PHP Code:
class Product_Model extends CI_Model
{
    public function 
getItems()
    {
        
//your query to get the products
        
$objCollection = new Product_Collection($query->result("Product_Object"));
        return 
$objCollection;
    }


Range Collection
PHP Code:
class Range_Collection extends Array_Object
{
    private 
$arrRangeObjectsByKey = array();

    public function 
__construct($arrObjects = array())
    {
        
parent::__construct($arrObjects);
        
$this->setRangeArrayByKey($arrObjects);
    }
    
    private function 
setRangeArrayByKey($arrObjects)
    {
        foreach(
$arrObjects AS $objRange)
        {
            
$this->arrRangeObjectsByKey[$objRange->range_key] = $objRange;
        }
    }

    public function 
addProductCollection(Product_Collection $objCollection)
    {
        foreach(
$objCollection AS $objProduct)
        {
            
$this->arrRangeObjectsByKey[$objProduct->range_key]->addProduct($objProduct);
        }
    }

Range_Object
PHP Code:
class Range_Object
{
    private 
$objProductsCollection;
    
    public function 
__construct()
    {
        
$this->objProductCollection = new Array_Object;
    }
    
    public function 
addProduct(Product_Object $obj)
    {
        
$this->objProductCollection->append($obj);
    }
    
    public function 
getProductCollection()
    {
        return 
$this->objProductsCollection;
    }
    
    public function 
hasProductItems()
    {
        return (
$this->objProductsCollection->count 0)    ?    true    :    false;
    }

Product Collection and DataObjects
PHP Code:
class Product_Collection extends Array_Object
{
    
}


class 
Product_Object
{
    

your controller
PHP Code:
class Range_Controller
{    
    public function 
displayRanges()
    {
        
$this->load->model("Range_Model");
        
$arrViewData = array(
            
"objCollection" => $this->Range_Model->getItems()
        );
        
$this->load->view("range_view"$arrViewData);
        
        
        
    }


and your view

PHP Code:
<table>
<?
php
    
foreach($objCollection AS $objRange)
    {
?>
 <tr>
    <td><?=$objRange->name?></td>
    <td><?=$objRange->description?></td>
    <td><?=$objRange->image?></td>
 </tr>
<?php
    
if ($objRange->hasProductItems())    :
        foreach(
$objRange->getProductCollection() AS $objProduct)    :
?>
 <tr>
    <td><?=$objProduct->size?></td>
    <td><?=$objProduct->colour?></td>
    <td><?=$objProduct->price?></td>
 </tr>
<?php
        
endforeach;
    endif;
    }
?>
</table> 
Reply


Messages In This Thread
RE: At what point do you seperate things into different models? - by sintakonte - 06-22-2016, 05:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB