[eluser]flokky[/eluser]
I'm trying to find out what is the best approach to load a model within a model. I've searched and found some threads, but it doesn't quite answer my particular problem.
This is my (simplified) database structure:
Table: newscontent
id, title, content, date, category_id
Table: newscategory
id, categoryname
newscontent:category_id refers to newscategory:id.
How can I fetch the categoryname using the category_id? Sure, I can write a getCategoryName with the supplied category_id, but when using activeRecords I don't know how I can access the category_id, since my model doesn't have any modifiers (like in Java)
Code:
class News extends Controller {
function News(){
parent::Controller();
$this->load->model('newssource', 'newssource');
$this->load->model('newscontent', 'newscontent');
$this->load->model('newscategory', 'newscategory');
$this->load->helper('date');
}
function index(){
//How can I fetch the category using getNewsContent?
$news = $this->newscontent->getNewsContent($fields=null, $limit=null, $where=null);
$data['news'] = $news->result();
//Fetches all categories
$categories = $this->newscategory->getNewsCategories($fields=null, $limit=null, $where=null);
$data['categories'] = $categories->result();
$this->load->vars($data);
$this->load->view($this->_container);
}
}
The model newscontent looks like this:
Code:
class Newscontent extends Model {
function Newscontent(){
parent::Model();
$this->_prefix = $this->config->item('FAL_table_prefix');
$this->_table=$this->_prefix.'newscontent';
}
function getCategoryName() {
$CI =& get_instance();
$CI->load->model('newscategory','newscategory');
$cat = $CI->newscategory->getCategoryById($this->category_id);
$cat2 = $cat->result();
return $cat2;
}
function getNewsContent($fields=null, $limit=null, $where=null) {
($fields != null) ? $this->db->select($fields) :'';
($where != null) ? $this->db->where($where) :'';
($limit != null ? $this->db->limit($limit['start'], $limit['end']) : '');
return $this->db->get($this->_table);
}
...
}