Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorpweil
     
    I have a simple database of 'resources', each of which has a contact, category, host, and location. Each of these attributes is linked to the resources table by a field named contact_id, category_id, host_id, and location_id.

    I'm probably doing this incorrectly, but:

    In the the resource model:

    var $belongs_to = array('contact', 'category', 'host', 'location');

    In the contact, category, host, and location models:

    var $has_many = 'resources';

    Does Active Record assume that fields with names like these are foreign keys, or does one need to spell these out explicitly in the resources controller? I'm still working with the scaffold files, and right now I just want to see all fields list when I view listing.tpl (SELECT * FROM resources LIMIT 20) But right now, listing.tpl does not even return the contact_id, category_id, host_id, and location_id columns:

    Resource columns: Array
    (
    [0] => title
    [1] => description
    [2] => abbrev
    [3] => url
    [4] => updated_at
    [5] => created_at
    )

    After this I assume that I need to change the Find method to implement a join and get the values in the other tables...I'd like to know how to do this, too.
    •  
      CommentAuthorbermi
     

    Akelos will generate the join for you as long as you tell the finder which associations to include.

    In your case:

    $this->Resources = $this->Resource->find('all', array(''include'=>'contact,category,host,location', 'limit'=>20));
    
    • CommentAuthorpweil
     
    Thanks Bermi. Ok, I understand this approach (it seems similar to Rails), but when I tried this I received the following error:

    Notice: Undefined property: Resource::$contact,category,host,location in /Users/pweil/akelos/lib/AkActiveRecord/AkAssociatedActiveRecord.php on line 256

    Fatal error: Call to a member function getAssociatedFinderSqlOptions() on a non-object in /Users/pweil/akelos/lib/AkActiveRecord/AkAssociatedActiveRecord.php on line 256

    Sorry to sound so lost -- it's a little hard finding one's way through this in the early going.
    •  
      CommentAuthorbermi
     

    sorry pweil, this syntax was not valid until now

     $this->Resources = $this->Resource->find('all', array('include'=>'contact,category,host,location', 'limit'=>20));
    

    you can update your working copy to rev.578 or use the array version like

     $this->Resources = $this->Resource->find('all', array('include'=>array('contact','category','host','location'), 'limit'=>20));
    
    • CommentAuthorpweil
     
    Ok, that works. Thanks Bermi! And thanks for all of your work on this project. - Peter
    • CommentAuthorinsanet
     

    There is someway to do this?

    $this->Resources = $this->Resource->find('all', array(''include'=>'contact',array('location'=>array('include'=>'country')), 'limit'=>20));
    
    • CommentAuthorKaste
     

    right now, unfortunately we dont have "eager loading of deep associations".