Welcome Guest, Not a member yet? Register   Sign In
Resuable models
#14

[eluser]n0xie[/eluser]
Well jedd did solve the problem for you. It basically boils down to this: you can't wrap good code around bad design.

You could solve this kind of problem with an 'interface'. Still it doesn't matter how you build it, unless you provide us with a better 'example' for all purposes this will still be bad design.

comment_model.php
Code:
/**
* Warning: HERE BE DRAGONS!!
* don't use if you have any understanding of how relational databases work
* since this issue is void if you merge the tables and add a 'type' identifier
*/
class Comment_model extends Model {

    private $table;

    function Comment_model()
    {
        parent::Model();
    }

    /**
     *  here we offer 2 ways to access the model
     *  an 'interface' if you will
     *  although obviously real OOP interfaces
     *  do this sort of magic 'automatically'.
     *
     */

    public function get_magazine_comment($id = NULL, $limit = 1)
    {
        $this->set_table('table_magazine');
        return $this->get_comment($id, $limit);
    }

    public function get_book_comment($id = NULL, $limit = 1)
    {
        $this->set_table('table_book');
        return $this->get_comment($id, $limit);
    }

    /**
     *  Let's keep this part hidden from the rest
     */
    private function set_table($table)
    {
        $this->table = $table;
    }

    private function get_comment($id, $limit = 1)
    {
        $this->db->where('id', $id);
        $this->db->limit($limit);
        $query = $this->db->get($this->table);

        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return FALSE;
        }
    }
}

Now if you want to abuse the relational database even more you could add endless amount of tables and just have to add another 'interface' function. It's KISS and DRY !


Messages In This Thread
Resuable models - by El Forum - 08-04-2009, 06:45 AM
Resuable models - by El Forum - 08-04-2009, 07:23 AM
Resuable models - by El Forum - 08-04-2009, 07:28 AM
Resuable models - by El Forum - 08-04-2009, 07:28 AM
Resuable models - by El Forum - 08-04-2009, 07:30 AM
Resuable models - by El Forum - 08-04-2009, 07:37 AM
Resuable models - by El Forum - 08-04-2009, 07:45 AM
Resuable models - by El Forum - 08-04-2009, 10:27 AM
Resuable models - by El Forum - 08-04-2009, 12:05 PM
Resuable models - by El Forum - 08-04-2009, 01:47 PM
Resuable models - by El Forum - 09-07-2009, 04:13 PM
Resuable models - by El Forum - 09-07-2009, 04:35 PM
Resuable models - by El Forum - 09-08-2009, 01:05 AM
Resuable models - by El Forum - 09-08-2009, 04:01 AM
Resuable models - by El Forum - 09-08-2009, 04:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB