• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
At what point do you seperate things into different models?

#11
(06-20-2016, 11:10 AM)PaulD Wrote: Wow, that is a huge number of tables. My biggest site has about 20. What on earth do they need so many tables for?

I'm not allowed to say, but it is a property management related website.
Reply

#12
(06-20-2016, 04:45 AM)CINewb Wrote: Let's say you have a model called "Range"

A Range model might consist of a name, a description, and an image.
...

It's got me thinking, should Range and Product all be part of the same model?  The problem with that approach is that you might have a "Customer" and an "Order" model to represent customers and purchases.  Customers are related to Orders, and Orders are related to Products, and Products are related to Ranges.  At some point there are going to be requirements to join some or all of this information together in different controllers and views.  You could therefore end up with one giant model which encompasses everything, which would not be good either.

Thoughts?

I prefer prefixing Controllers, Models and Views with C_, M_ and V_ respectively  because the files can be immediately recognised using Sublimes list of files. Using this  grouping configuration avoids confusion.

Just my Song Satang thoughts Smile
Reply

#13
(06-20-2016, 11:14 AM)spjonez Wrote:
PaulD Wrote:Wow, that is a huge number of tables. My biggest site has about 20. What on earth do they need so many tables for?

We have 139 tables and 37 models in our app. The number tends to balloon after a few years of adding new features to the same project.

I've seen small companies have 200+. I myself am well over 100. 

When you have a site with a lot of role based micromanagement features on both user and user_group levels, things tend to escalate!
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#14
I would like to add something I consider important: https://support.google.com/manufacturers...4116?hl=en

"Throughout this article, you will see the terms item and product. Please note that we use the term item to describe a single product variant. Items are similar to products, but we assume that products can occur in multiple variations. These variations, or “product variants”, can differ by color, size, or style. In other words, an item refers to a single variation of a product. Each entry in your product data file should reflect a single item, or product variant."

My interpretation is that every item that the customer picks up should have an unique ID, it could be the primary key directly (of the item, not the product) or some unique external key. An item (a concrete product with its concrete option values) should provide its own URL/page that is to be indexed. The item ID should be stored within the shopping cart and later within the created order, thus it will be always known what exactly the client has ordered, no chance for a mistake (different color, size, etc.).
Reply

#15
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.