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

    Hello,

    I'm having the following problem and I'm horribly stuch with this. Guess it's me doing something wrong and it will probably be an easy task...

    I have the following models / associations:

    Group: has many users
    User: belongs to group, has many locations
    Location: belongs to user, has many devices
    Device: belongs to location
    

    In the device_controller I'm trying to get a list of devices, filtered by an specific group (let's say, for example, the group id of the user that's currently logged in that's stored in a session var) so:

    $options = array( 'conditions' => array( '_user.group_id = '.$this->credentials->group_id ), 'include' => array('location','user'));
    $this->devices =& $this->Device->find('all',$options);
    

    That gives me an error:

    Fatal error: Call to a member function on a non-object in ...../includes/akelos/lib/AkActiveRecord/AkAssociatedActiveRecord.php on line 256
    

    I've tried to use dbug(); but no SQL is generated. And deleting 'user' of the include array it works, but obviously it can't find the user table and fails.

    Any help would be really appreciated. Thanks in advance.

    Jose Garcia

    •  
      CommentAuthorbermi
     

    Hi Jose,

    The only thing that seems strange on your comment is that "User belongs to User". Can you post your models, installers and unit test so we can investigate further?

    • CommentAuthorasejua
     

    Ups, seems I made a mistake, I've edited the post changing that for what's correct. User: belongs to Group :)

    Here you have the models:

    group.php

    class Group extends ActiveRecord {
    var $has_many = array('users');
    }
    

    user.php

    class User extends ActiveRecord {
    var $belongs_to = array('group','role');
    var $has_many = array('locations','operators','sensors','alerts','system_messages');
    }
    

    location.php

    class Location extends ActiveRecord {
    var $belongs_to = array('user');
    var $has_many = array('devices');
    }
    

    device.php

    class Device extends ActiveRecord {
    var $belongs_to = array('location');
    var $has_many = array('ports','site_logs');
    }
    

    Here the installers (I've cut some non-important fields for better reading):

    group_installer.php

    $this->createTable("groups","
    id integer(11) UNSIGNED NOTNULL AUTOINCREMENT PRIMARY,
    name varchar(255) NOTNULL DEFAULT '',
    description
    ");
    

    user_installer.php

    $this->createTable("users","
    id integer(11) UNSIGNED NOTNULL AUTOINCREMENT PRIMARY,
    username string(15) NULL,
    password string(255) NULL,
    group_id integer(11) UNSIGNED NULL,
    role_id integer(11) UNSIGNED NULL,
    ");
    

    location_installer.php

    $this->createTable("locations","
    id integer(11) UNSIGNED NOTNULL AUTOINCREMENT PRIMARY,
    name string(255) NOTNULL DEFAULT '',
    user_id integer(11) UNSIGNED NOTNULL
    ");
    

    device_installer.php

    $this->createTable("devices","
    id integer(11) UNSIGNED NOTNULL AUTOINCREMENT PRIMARY,
    location_id integer(11) UNSIGNED NOTNULL,
    name string(255) NOTNULL DEFAULT '',
    ...
    ");
    

    The test cases unfortunatelly are a thing I still don't know well how to use (and I should learn to, I know :D) I'm used to debug the application using Zend IDE, so, sorry for not having test cases. I mean, the test cases I have contains what the scaffolding initially put on it.

    • CommentAuthorKaste
     

    Device is not associated with user.

    • CommentAuthorasejua
     

    I don't know the depths of ActiveRecord, obviously :) but I'm trying to learn. Will try it using findBySql.

    Thanks.

    • CommentAuthorasejua
     

    Solved it using findBySql function and something like this:

    $this->devices =& $this->Device->findBySql("SELECT devices.* FROM devices,locations,users,groups
    WHERE devices.location_id = locations.id
    AND locations.user_id = users.id
    AND users.group_id = groups.id
    AND groups.id = ".$this->credentials->group_id);
    

    Pretty simple, maybe not the perfect solution, but it works :)

    • CommentAuthorasejua
     

    The previous solution didn't worked well, so after reading some posts of the forum I reached the following solution, just if it helps somebody else :)

    In the device model I added a field called 'created_by' and an association to it pointing to user naming it author:

    var $belongs_to = array('location', 'author'=>array('class_name'=>'User','primary_key_name'=>'created_by'));
    

    Being now linked all the needed models (thanks Kaste) now the listing in device_controller did the work:

    $_filter  = array('conditions' => array('_author.group_id = ?',$this->credentials->group_id));
    $_include = array('include' => array('location','author'));
    $options  = array_merge($_filter,$_include);
    $this->devices =& $this->Device->find('all',$options);
    

    And that's all ;)