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

[eluser]m4rw3r[/eluser]
I could add an option to let habtm relations avoid the usage of subqueries, but I don't know if I would be slowing down the adaption of MySQL 5 Smile.

Tell me if you want to have an option to turn it off.

[eluser]sophistry[/eluser]
what would it use instead? as in, what did IR use before when it worked on the old v4 mysql?

i hate to admit it, but it would be easier to be able to leave mysql alone and have some way of disabling sub-selects in IR's habtm.

but, as they say "beggars can't be choosers!" ;-)

of course, i don't need an option in the distribution code, just a hint where to hack it into my own set up.

thanks!

[eluser]m4rw3r[/eluser]
The habtm code which chooses if it should use subqueries lies in ignitedrecord/relproperty_habtm.php.
On line 204 (current svn rev), it chooses what to use depending on if it is an ActiveRecord instance it has or an IgnitedQuery instance.
So just remove the if( ! is_a(... and else block.

[eluser]sophistry[/eluser]
ok, did that (just added TRUE OR to the line 204 to always execute the non subquery code)
Code:
if(TRUE OR ! is_a($query, 'IgnitedQuery'))
and now i have some kind of string cat issue:
Code:
A Database Error Occurred
Error Number: 1054

Unknown column 'group_id' in 'field list'

SELECT `group_id` FROM `beings` `_groups` WHERE `being_id` = '1'

there is a column group_id in the beings_groups table so that is not the problem... the problem seems to be where the name of the join table is constructed.

where is the concatenation on beings and _groups taking place? why is it creating a string with a space in it?

my beings table yaml model factory code is simple:
Code:
table: beings    # necessary
habtm:
    name: groups

thanks!

EDIT: I can make the error go away and come back by adding a tab and/or space in the yaml file. But, then the related->get() method returns nothing. i tested the sql directly and it finds the record ok. so something is wrong in the parsing of the yaml, i *think*.

[eluser]m4rw3r[/eluser]
The name of the join table is constructed in the get_join_table() in the same file as the one you edited.
But it seems like you've got two tables there, and it may be a bug in the code (the build_query() method seems to add '_groups' and then you've already got beings from somewhere else).

Poke around in there, and please tell me what you find out.

[eluser]sophistry[/eluser]
ok, got it. the HABTM query is working.

the yaml file comment "# necessary" was causing the parser to do something weird.

this is my current stripped-down being.yaml
Code:
table:beings
habtm:
name:groups

only one space to indicate the indent on the habtm: name: section. EDIT: the forum stripped out the space before the string " name:groups"

maybe the yaml parser should just strip out the comment string in a separate run before parsing? it looks like the yaml parser regex might benefit from that kind of pre-processing. it would become a little more readable anyhow.

thanks for your help! i think i'm on my way now with the new IR set up!

[eluser]sophistry[/eluser]
Here's what I've got so far. I pretty much used the quick start set up I'd done a few months ago for an earlier version of IR, but I changed the name of the users table to beings - for various reasons.

This shows how to do a basic HABTM using yaml files.
Code:
<?php

/*

yaml files in APPPATH."definitions/":

file: being.yaml
-------------
table: beings
habtm:
    name: groups
-------------

file: group.yaml
-------------
table: groups
habtm:
    name: beings
-------------

file: being_group.yaml
-------------
table: beings_groups
has_many:
    name: groups
    name: beings
-------------

*/


class Ir_test_habtm extends Controller {

    function Ir_test_habtm()
    {
        parent::Controller();
        // manually load the base model
        $this->load->model('ignitedrecord/ignitedrecord');
        
        // create the models based on yaml files
        foreach(array('being','group','being_group') as $model)
        {
            $this->$model = IgnitedRecord::yamlfactory(APPPATH."definitions/$model.yaml");
        }
    }
    
    function index()
    {    
        // find id 1
        $being1 = $this->being->find(1);
        print_r('user with id #1: '.$being1->first_name.'<hr>');
        
        // get the groups that being id 1 belongs to
        echo 'being '. $being1->first_name .' belongs to groups:<br>';
        $groups = $being1->related('groups');
        foreach($groups->get() as $gr)
        {
            p($gr->group_name.'<br>');
        }
        
        // show all beings
        echo '<hr> all beings:<br>';
        $beings = $this->being->find_all();
        foreach($beings as $b)
        {
            print_r($b->first_name.'<br>');
        }
    }
    
    // demo code
    function add_being($f,$l)
    {
        $rec = $this->being->new_record(array('first_name' => $f, 'last_name' => $l));
        $rec->save(); // adds a new row to the table
    }
    
    // demo code
    function add_group($g,$t)
    {
        $rec = $this->group->new_record(array('group_name' => $g, 'group_type' => $t));
        $rec->save(); // adds a new row to the table
    }
    
    // demo code
    function add_being_to_group($bid,$gid)
    {
        $rec = $this->being_group->new_record(array('being_id' => $bid, 'group_id' => $gid));
        $rec->save(); // adds a new row to the table
    }
    
}

/* End of file ir_test_habtm.php */
/* Location: ./system/application/controllers/ir_test_habtm.php */

[eluser]MarcoITA[/eluser]
Hi m4rw3r,

a quick request. Would you consider introducing the opportunity to fecth additionale columns from the join table in a habtm relation?

Example:

articles
id
title
body
...

pages
id
name
layout
...

articles_pages
page_id
article_id
zone
order


As of now fetching the related articles for a given page just list them, but it would be useful to also have the "blue" join table's columns in the returned rows (and optionally also to sort by them?).

Could be done "user side" adding a setting key in the $habtm array like:

Code:
$habtm = array('table'=>'articles','join_cols'=>array('zone','order'));

[eluser]m4rw3r[/eluser]
It is possible to do this right now, like this:
Code:
class Artice extends IgnitedRecord{
    var $has_many = 'associations';
}
class Association extends IgnitedRecord{  // EDIT: you can call this model whatever you want
    var $belongs_to = array('article', 'page');
}
class Page extends IgnitedRecord{
    var $has_many = 'associations';
}

// controller, or whatever
$art = $this->article->find(1);
$pages_by_title = $art->related('associations')->join_related('page')->order_by('page_title')->get();
$pages_by_order = $art->related('associations')->join_related('page')->order_by('order')->get();

foreach($pages_by_title as $page){
    // the pages data is prepended with "page_"
    echo $page->page_title;
}
It's a bit clumsy, so your suggestion is a good one which I will give some thought to.

Also the Through relationship type would simplify if you only want the pages some times and the full associations other times.

And I will investigate the object splitting again (so you get the data in separate objects).

[eluser]MarcoITA[/eluser]
Tried, but I only get the "associations" rows... looking to the intermediate object looks like that the join_related method does not actualy change anything. Joined table fields (page in your example) are not added to the records from the association table...

Right now I'm cycling through the results of the ->related('associations') method to get all the articles fields.

Next days I will try to hack the relproperty_habtm.php source to implement something basic. If will succeed I will eventually post code here...




Theme © iAndrew 2016 - Forum software by © MyBB