[eluser]CtheB[/eluser]
Hi all,
I am new to the forums and CI. I am trying to get some things work. I don't know exactly how to handle IgnitedRecord. Can somebody help me figuring out how to pass id's in an related query:
Code:
// news_model.php
class News_Model extends IgnitedRecord {
public $has_and_belongs_to_many = 'tags';
public function __construct()
{
parent::IgnitedRecord();
}
public function latest_posts($num)
{
$this->where('status', 'published');
$this->limit($num);
$this->order_by('date_posted', 'DESC');
$this->posts = $this->find_all();
foreach ($this->posts as $post)
{
$this->id = $post->id;
return $this->posts;
}
}
public function get_tags()
{
$recs = $this->find($this->id);
$u = $recs->related('tags');
return $u->get();
}
}
// news.php
class News extends Controller {
public $num_to_display = 10;
public function __construct()
{
parent::Controller();
$this->template['module'] = "news";
$this->load->model($this->template['module'], 'news_model', NULL, true);
}
public function index()
{
$this->template['title'] = 'News';
$this->template['posts'] = $this->news_model->latest_posts($this->num_to_display);
$this->template['thetag'] = $this->news_model->get_tags();
$this->layout->load($this->template, 'index');
}
}
// index.php
<h1>Latest News Posts</h1>
<?php $count = count($posts); $i = 1; foreach ($posts as $post):?>
<h2 class="news"><a >date_posted).'/'.$post->uri)?>"><?=$post->title?></a></h2>
<p class="posted">Posted: <?=date('d.m.Y', $post->date_posted)?></p>
<?php if ($thetag): ?>
<p>Tags: <?php foreach ($thetag as $tag): ?><a >tag)?>"><?=$tag->tag?></a> <?php endforeach; ?></p>
<?php endif; ?>
<div><?=$post->body?></div>
<?php $i++; endforeach;?>
Everything is ok, except for the $tag->tag. Foreach News item, i'm getting the tags of the first news item.
I know i have to change the latest_posts() and the get_tags() functions.
I've tried everything. It must be something like this:
Code:
function latest_posts($num)
{
$this->where('status', 'published');
$this->limit($num);
$this->order_by('date_posted', 'DESC');
$post['news'] = $this->find_all();
foreach ($this->posts as $post)
{
$post['tags'] = $this->get_tags($post->id)
}
return $post;
}
function get_tags($news_id)
{
$recs = $this->find($news_id);
$u = $recs->related('tags');
return $u->get();
}
(But ofcourse it's not working because i'm dealing with objects and not with an array.
Please note i don't want it to work with an array. I just want to past the correct $post->id to the get_tags() function. (so the tags will be different for each post).
I had this working before switching to IgnitedRecord. But with ignitedrecord i can't figure it out.
Thanx in advanced for the help. Maybe with a little more experience i can help other people in the near future.
Greets Carlos.