Welcome Guest, Not a member yet? Register   Sign In
Many-to-Many Relationships & Active Record Class
#4

[eluser]Chris Newton[/eluser]
This is how I ended up solving it; (view & controller code is super basic for testing purposes)

controller:
Code:
function index()
    {
        $this->load->model('news', '', TRUE);
        $data['news']=$this->news->readNews();        
        $this->load->view('welcome_message',$data);
        
    }

model:

Code:
function readNews(){
        $this->db->select('*');
        $this->db->from('news');
        $this->db->orderby('id','desc');
        $news_info=$this->db->get();
        $my_news=array();
        foreach ($news_info->result() AS $key =>$value){
                        $my_news[$key]['news']=$value;
                        $this->db->select('name, id');
                        $this->db->from('news_to_categories');
                        $this->db->join('news_categories', 'news_to_categories.category_id=news_categories.id');
                        $this->db->where('news_id',$value->id);
                        $query2=$this->db->get();
                        $my_news[$key]['categories']=array();
                        foreach($query2->result() AS $cats){
                            $my_news[$key]['categories'][]=$cats;
                        }
        }
        return $my_news;
    }

Doesn't seem ideal, but It works. I keep trying to come at it from different angles, and nothing better comes to mind. I suppose I could (maybe should?) create 2 functions in the model, one to read the news items, and the other to read the categories, and then join them in the controller. Thoughts?
view:

Code:
foreach ($news AS $key=>$item){
    foreach ($item['categories'] AS $key2=>$cat){
         echo $cat->name;
         echo $cat->id;
         echo br(1);
    }
    echo br(2);
    echo "subject ".$item['news']->subject;
    echo br(2);
    echo "date ".$item['news']->date;
    echo br(2);
}


Messages In This Thread
Many-to-Many Relationships & Active Record Class - by El Forum - 11-27-2007, 09:00 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-27-2007, 10:07 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-27-2007, 10:16 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-27-2007, 11:32 PM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 02:44 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 03:00 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 08:41 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 09:00 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 10:07 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 11:02 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 11:49 AM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 12:33 PM
Many-to-Many Relationships & Active Record Class - by El Forum - 11-28-2007, 08:46 PM
Many-to-Many Relationships & Active Record Class - by El Forum - 12-05-2007, 11:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB