Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

[eluser]m4rw3r[/eluser]
@Cannyp:

join_related() must be called when constructing the query, also related() only creates a query object - it does not execute the query - so you have to call get() on that object in turn.

Code:
// Example:
$deliveries = $order->related('delivery_points')->join_related('file', 'name')->where('delivery_points.deleted', '0')->get();
// you now have the filename in file_name
Also, it seems like you're making your relation definitions overly complicated, usually it is sufficient with the relation name.

PS. Why are you using the model config option for relations? It does not exists (I'm assuming latest svn, but it disappeared quite a long time ago).

@porky207:

Do you try to load the class which extends IR_record via Loader::library()?

That is not possible, because Loader::library() tries to instantiate the class.
So use require_once APPPATH.'libraries/Your_custom_record.php';

[eluser]Cannyp[/eluser]
Thats great.

I started off naming all my relations becuase that was the only way I could get join_related to work a while ago, but I now have a slightly (very slight) better understanding off how it fits together now and have gone through and reverted to default relation names.

On another note, Im sure this has been asked before but I cant find it on the forum or google so. Is it possible to wedge in calculated fields to the object?

say, I want to load up the object and have all the columns plus a calculated field which subtracts the difference between two date columns in that row. Is this possible or do I have to hand craft the SQL and use $obj->find_all_by_sql() ?

Ideally I want to get the calculated field into the following object.

Code:
// select delivery_points.*, UNIX_TIMESTAMP(finished) - UNIX_TIMESTAMP(started) as thetime from delivery_points...

$deliveries = $order->related('delivery_points')->join_related('file', 'name')->where('delivery_points.deleted', '0')->get();


Thanks again.

[eluser]porky207[/eluser]
Alright: I've loaded the child class using require_once and (drumroll) we're up & running. Thanks!

[eluser]sophistry[/eluser]
@Cannyp... i believe that IR "Behaviors" is what you are looking for.

[eluser]m4rw3r[/eluser]
A behaviour isn't required, and wouldn't work there (yet, I need to come up with a way to add hooks to relations - nicely).

Instead, use IgntedQuery's select() method (IgnitedQuery ~ CI's ActiveRecord).
All query building objects inherit from IgnitedQuery, which gives them quite a lot of ability for modifications:
Code:
$deliveries = $order->related('delivery_points')
        ->select('UNIX_TIMESTAMP(finished) - UNIX_TIMESTAMP(started) as thetime', false)
        ->join_related('file', 'name')
        ->where('delivery_points.deleted', '0')
    ->get();
You'll then have a new property on the $deliveries objects called "thetime".

[eluser]sophistry[/eluser]
ah, yes, i was not paying attention... i see the request for calculated field capability is specifically for related objects. however, correct me if i am wrong that you could provide a slew of calculated fields by using behaviors in a "normal" IR object.

assume by "add hooks to relations - nicely" you mean triggering hooks without causing undue resource usage?

[eluser]m4rw3r[/eluser]
Yep, and it should also be:
- neatly integrated with the rest of the hooks
- easy to add callables to them (as easy as the normal ones)
- not messy in IR code

[eluser]Cannyp[/eluser]
Fantastic.

Thanks all. (happy new year also Big Grin)

[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>
&lt;?php $count = count($posts); $i = 1; foreach ($posts as $post):?&gt;
<h2 class="news"><a >date_posted).'/'.$post->uri)?&gt;">&lt;?=$post->title?&gt;</a></h2>
<p class="posted">Posted: &lt;?=date('d.m.Y', $post->date_posted)?&gt;</p>
&lt;?php if ($thetag): ?&gt;
<p>Tags: &lt;?php foreach ($thetag as $tag): ?&gt;<a >tag)?&gt;">&lt;?=$tag->tag?&gt;</a> &lt;?php endforeach; ?&gt;</p>
&lt;?php endif; ?&gt;
<div>&lt;?=$post->body?&gt;</div>
&lt;?php $i++; endforeach;?&gt;

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.

[eluser]m4rw3r[/eluser]
I can immediately see an error you have made; the return in your foreach loop (what I can say about that: PHP is not python (It comes very close to python logic, IMOH)).

I would do the fetching of the related tags through the usage of join_related():
Code:
// model
public function latest_posts($num)
{
    // by using this, you'll have all tags in an array in the property tags:
    $this->join_related('tags');

    $this->where('status', 'published');
    $this->limit($num);
    $this->order_by('date_posted', 'DESC');

    return $this->find_all();
}

// controller
$this->template['title'] = 'News';
$this->template['posts'] = $this->news_model->latest_posts($this->num_to_display);
$this->layout->load($this->template, 'index');

// view:
&lt;?php foreach ($posts as $post):?&gt;
<h2 class="news">&lt;?=anchor('blog/'.$post->date_posted.'/'.$post->uri, $post->titl)?&gt;</h2>
<p class="posted">Posted: &lt;?=date('d.m.Y', $post->date_posted)?&gt;</p>
&lt;?php if( ! empty($post->tags)): ?&gt;
// here we loop the tags:
<p>Tags: &lt;?php foreach ($post->tags as $tag){ echo anchor('tags/list/'.$tag->id, $tag->tag); } ?&gt;</p>
&lt;?php endif; ?&gt;
<div>&lt;?=$post->body?&gt;</div>
&lt;?php endforeach;?&gt;
This code performs only one (or two, depending on if columns need to be listed or not) queries, so it is very effective in that regard.
If you would have used $tags = $post->related('tags')->get() for each post, you would have called n + queries (where n = number of posts).

So you can get rid of the get_tags method from the model, it isn't needed.
And keep in mind, those record objects are used for fetching related records, not the model (usually).
Also, you can add your own methods to the record objects too.




Theme © iAndrew 2016 - Forum software by © MyBB