• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resuable models

#9
[eluser]Chad Fulton[/eluser]
I think that Treeda has an interesting question here, and it is Jedd etc. who are attempting to fit a square peg in a round hole.

In my view, the idea that there ought to be a "generic" database table to hold both sorts of comments is a bad practice, since there is no semantic reason why comments regarding magazines and comments regarding books ought to be the same, or stored in the same table.

For one thing, it needlessly complicates growth down the road. What if there are comment fields specific to books that don't apply to magazines, or vice versa?

For me, the clear answer in this situation would be creating a Comment model, and extending it with BookComment and MagazineComment (as the original comment suggested). The whole point of extensions is just for this sort of problem: how to deal with two things that share common basic elements but differ in their specific implementations.

The idea, I assume, is that there are both common functionality as well as specific functionality. Although if they truly are exactly the same, and there is no chance they will deviate, then I agree that it might just be easier to combine them into a single one.

Here's some example code (I dunno what your actual plan is, but this should shed some light, I hope):

models/Comment.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends Model {

    public $id;
    public $comment;

    public function get($id, $table) {
        $this->db->where('id', $id)
                 ->limit(1)
                 ->get($table);
        foreach($this->db->row_array() as $key=>$value) {
            $this->$key = $value;
        }
    }

    // Common functionality here

}

models/BookComment.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('Comment.php');

class BookComment extends Comment {
    public $book_id;

    public function get($id) {
         parent::get($id, 'bookComment');
    }

    // BookComment specific functionality here
}

models/MagazineComment.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('Comment.php');

class MagazineComment extends Comment {
    public $mag_id;

    public function get($id) {
         parent::get($id, 'magazineComment');
    }

    // Magazine Comment functionality here
}


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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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