Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthormrcyril
     
    Hi All

    I'm having some trouble with eager loading. My domain model looks like this:

    User : belongs_to Client
    Client: has_many sections,users
    Section: belongs_to filter, client
    Filter: has_many sections,clients

    I wish to obtain Section Objects and eagerly load Filter associations using the User Object as the root.

    $User->client->section->find('all',array('include' => 'filter'))

    The SQL produced uses table name aliases but not in the WHERE clause.

    Fri, 11 Apr 2008 12:32:06 +0100 [MESSAGE] find with associations: SELECT __owner.id AS __owner_id, __owner.filter_id AS __owner_filter_id, __owner.path AS __owner_path, __owner.period AS __owner_period, __owner.html_table AS __owner_html_table, __owner.html_chart AS __owner_html_chart, __owner.csv AS __owner_csv, __owner.totals AS __owner_totals, __owner.updated_at AS __owner_updated_at, __owner.created_at AS __owner_created_at, __owner.client_id AS __owner_client_id, _filter.id AS _filter_id, _filter.region AS _filter_region, _filter.country AS _filter_country, _filter.county AS _filter_county, _filter.updated_at AS _filter_updated_at, _filter.created_at AS _filter_created_at FROM sections AS __owner LEFT OUTER JOIN filters AS _filter ON __owner.filter_id = _filter.id WHERE sections.client_id = 1
    Fri, 11 Apr 2008 12:32:06 +0100 [MESSAGE] SQL Error: [1054] Unknown column 'sections.client_id' in 'where clause'

    Am I doing something wrong or could this be a bug?

    best

    Cyril
    •  
      CommentAuthorbermi
     

    $User->Client->Section holds an instance of AkHasmany, which is used for loading associated models into $User->Client->Sections. Keep in mind that it does not hold an Active Record.

    If you want eager loading into an association you should try.

    Client: has_many sections, users (include => 'filter')

    Let me know it it works, otherwise a unit test and a bug report will help us fix the issue

    • CommentAuthormrcyril
     
    That works for me, thanks Bermi

    I now have:
    var $has_many = array('users','sections' => array('include' => 'filter')).

    This does mean now that I will always get eagerly loaded filters every time I want sections via clients. There wouldn't be an option to control this per use-case, not that in this case there would be any practical advantage of having this control, just thinking out loud really.

    cheers,

    Cyril
    •  
      CommentAuthorbermi
     

    You can create different associations like

    var $has_many = array('users',
        'sections',
        'sections_and_filters' => array('class_name'=>'Section', 'include' => 'filter'),
        );
    

    And then include the right association.

    You can also try overwriting the default constructor with

    function __construct()
    {
        if(....){
            $this->habtm = array(....);
        }else{
            $this->habtm = array(....);
        }
    
        // aditionally you can avoid loading associations by setting
        //$this->disableAutomatedAssociationLoading = true;
    
        $attributes = (array)func_get_args();
        return $this->init($attributes);
    }
    

    But this might not be necessary

    • CommentAuthormrcyril
     
    Thanks for the response, it all makes sense.

    I'm currently working on three projects using Akelos (2 Commerical, 1 non-profit) having previously worked with Java (Spring and Hibernate); I have no previous experience of Rails. I am enjoying the framework very much, lots to learn.

    All the best,

    Cyril
    • CommentAuthorKaste
     

    now issue #149