CodeIgniter Forums
Structure models for different types of products - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Structure models for different types of products (/showthread.php?tid=62776)



Structure models for different types of products - groovebird - 08-25-2015

Hi,

in my app i have 3 types of products (smartwatches, smartphone and tablets). I don't know how to structure my models.

All types have some properties in common (like a name, weight...) Then smarthphones and tablets both have some properties.

What is a good way to structure the models? I would create a product-model and every other type can inherit from that. But what to do with smartphones and tablets? Where i have to store the common properties?

PHP Code:
Product_model extends CI_Model {}
Smartphone_model extends Product_model {}
Tablet_model extends Product_model {} 

Another question:
How can i get all the data from 2 database tables for an smartphone? The smartphone data are in one table (only for smartphones) and the other common data (name and so on) are in another table. In the product model there is a method find_all(), but there i get only the data from the products table. The second table for the join would be smartphone or smartwatch, but how can i get the name dynamic?


RE: Structure models for different types of products - mwhitney - 08-25-2015

(08-25-2015, 12:20 PM)groovebird Wrote: What is a good way to structure the models? I would create a product-model and every other type can inherit from that. But what to do with smartphones and tablets? Where i have to store the common properties?




PHP Code:
Product_model extends CI_Model {}
Smartphone_model extends Product_model {}
Tablet_model extends Product_model {} 

PHP Code:
class Yourbase_model extends Product_model {}
class 
Smartphone_model extends Yourbase_model {}
class 
Tablet_model extends Yourbase_model {} 

Quote:Another question:
How can i get all the data from 2 database tables for an smartphone? The smartphone data are in one table (only for smartphones) and the other common data (name and so on) are in another table. In the product model there is a method find_all(), but there i get only the data from the products table. The second table for the join would be smartphone or smartwatch, but how can i get the name dynamic?

PHP Code:
class Product_model extends CI_Model

{
    protected $tableName '';
    protected $key 'id';

    protected $hasJoin false;
    protected $joinTable '';
    protected $joinCondition '';
    protected $joinType '';

    public function __construct()
    {
        parent::__construct();

        if ($this->hasJoin) {
            if (empty($this->joinTable) || empty($this->joinCondition)) {
                // ERROR, $joinTable and $joinCondition must be set to use the join.
                $this->hasJoin false;
            }
        }
    }

    public function find_all()
    {
        if ($this->hasJoin) {
            $this->db->join($this->joinTable$this->joinCondition$this->joinType);
        }

        // Setup the rest of the query...

        $query $this->db->get($this->tableName);

        // etc.
    }
}

class 
Yourbase_model extends Product_model
{
    protected $tableName 'common_data';

    public function __construct()
    {
        parent::__construct();

        // setup Yourbase_model
    }
}

class 
Smartphone_model extends Yourbase_model
{
    protected $hasJoin true;
    protected $joinTable 'smartphones';
    protected $joinCondition "{$this->tableName}.smartphone_id = {$this->joinTable}.{$this->key}";
    protected $joinType 'left outer';

    public function __construct()
    {
        parent::__construct();

        // Setup Smartphone_model
    }




RE: Structure models for different types of products - groovebird - 08-25-2015

Hi mwhitney,

BIG thank you for this great answer :-)

is there any documentation about that or did you code this whole stuff just now?


RE: Structure models for different types of products - mwhitney - 08-26-2015

(08-25-2015, 02:42 PM)groovebird Wrote: Hi mwhitney,

BIG thank you for this great answer :-)

is there any documentation about that or did you code this whole stuff just now?

Bonfire's BF_Model uses $table_name and $key properties, so I'm used to having something like that around in my models. It just made sense to me that if a couple of different models needed to use the same table for common data, then a different table for model-specific data, it would make sense to have a common base model with the same table, then a second property to tell it which table to join for the model-specific data.

The next step was to make sure the model could store everything necessary to make the join. Query builder's join() method requires a table and condition, and the type of join is optional. I initially considered using the if (empty($this->joinTable) || empty($this->joinCondition)) as the conditional in find_all(), but figured you might have multiple find methods, and it would be easier to create a boolean property to check in the find methods, while the constructor could use the more lengthy conditional to ensure the properties haven't been set to an invalid state. If you were to add setters for the join properties, it would probably be a good idea to set the $hasJoin property to true once both the joinTable and joinCondition were set. Generally, though, I would avoid allowing those to be set at run-time.