[eluser]Unknown[/eluser]
Dear fellow CI developer,
I'm in the process of developing a CMS (content management system) application. and typically CMS will have "users" that can login to edit the content of their article/post/message. I'm adding an "admin" control which also need to login, but they will be able to edit/approve user's article.
so my practice on this is to create 2 tables "admin" and "user" and two model "admin_model.php" and "user_model.php".
in the process of doing this I started to realize that they both share very similar behaviour. So I was thinking to apply the inheritance approach for this.
Since user is more basic and broad, I put user as the parent and admin as the child.
here is my two model code.
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends model
{
var $table_name;
// -- Constructor -----------------------------------------------
public function __construct()
{
parent::Model();
$this->table_name = "user";
}
public function get_user($id)
{
$query = $this->db->query("SELECT * FROM $this->table_name
WHERE id = '$id'");
$row = $query->row();
return $row;
}
}
?>
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once('user_model.php');
class admin_model extends User_model
{
var $table_name;
// -- Constructor -----------------------------------------------
public function __construct()
{
parent::__construct();
$this->table_name = "admin";
}
}
and in my controller I just load them by using "$this->load->model('Admin_model');"
I just wondering if my structure, the way I extends the parent, the way I assign my $table_name value is correct? should I declare the table_name value inside the constructor or outside? I still have to call the parent's constructor right? eventhough I don't want to use its "$table_name".
Also I don't have to write my function get_user($id) in my admin_model right? since I can just use the parent's function?
Thank you =)