Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord using model in another model
#1

[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);
    }
    
   ...
}
#2

[eluser]xwero[/eluser]
Why do you put the getCategoryName in the Newscontent model? It should be in the Newscategory model where it belongs.

If you want your Newscontent model to output the category name or another category value you are better of creating a two table query. It's not forbidden to use joins in models Smile
#3

[eluser]flokky[/eluser]
[quote author="xwero" date="1206547990"]Why do you put the getCategoryName in the Newscontent model? It should be in the Newscategory model where it belongs.[/quote]

At first I've done that, but after a while, I changed it. Since my content always belongs to a category, I would like it to 'automatically' fetch the categoryname.

Suppose we stick to your idea. How would you join those two together? Can you give an example because I'm stuck here ;-)
#4

[eluser]GSV Sleeper Service[/eluser]
SQL Join of some kind?

Code:
select id, title, content, date, categoryname
from newscontent
inner join
newscategory on newscategory.id =  newscontent.category_id
where newscontent.id = '$id'
#5

[eluser]xwero[/eluser]
Code:
function categoryNews($category)
{
     $this->db->select('t1.title, t1.content, t2.category_name');
     $this->db->from('newscontent t1');
     $this->db->join('newscategories t2','t2.id=t1.newscategories_id');
     $this->db->where('t1.newscategories_id',$category)
     return $this->db->get();
}
One sql statement is faster than two. But if you hold on to the one table one model rule you have to do it in the controller which is also slower.
#6

[eluser]flokky[/eluser]
Xwero & GSV Sleeper service: thank you both for helping me out here! I can't believe why I wasn't able to do something easy as that Smile




Theme © iAndrew 2016 - Forum software by © MyBB