Beau Beauchamp Sun Oct 16 15:09:37 -0400 2011

Subject: [RESOLVED] Zend Framework Models and Relationships

Okay so this isn't really working the way I expected. I want a linking table to give me a many-to-many relationship between orgs and users through the orgs_users table.

UPDATE: I just edited my code to autoload the models in Zend without the typical Zend path names included in the model name. The models are loading fine, but I'm just getting NULL when I instantiate the Orgs model and try to call its $orgs->users.

class Orgs extends ActiveRecord\Model {
    static $table_name = 'orgs';
    static $primary_key = 'org_id';
    static $has_many = array(
        array(
            'orgs_users', 
            'class_name' => 'OrgsUsers',
            'primary_key' => 'org_id'  <--- not sure about this?
        ),
        array(
            'users', 
            'through' => 'orgs_users', 
            'class_name' => 'Users',
            'primary_key' => 'user_id'
        )
    );
}

class Users extends ActiveRecord\Model {
    static $table_name = 'users';
    static $primary_key = 'user_id';
    static $has_many = array(
        array(
            'orgs_users', 
            'class_name' => 'OrgsUsers',
            'primary_key' => 'user_id'  <----- not sure about this?
        ),
        array(
            'orgs', 
            'through' => 'orgs_users', 
            'class_name' => 'Orgs',
            'primary_key' => 'org_id'
        )
    );
}

class OrgsUsers extends ActiveRecord\Model {
    static $table_name = 'orgs_users';
    static $primary_key = 'orgs_users_id';
   static $belongs_to = array(
        array(
            'orgs', 
            'class_name' => 'Orgs',
            'primary_key' => 'org_id'
        ),
        array(
            'users', 
            'class_name' => 'Users',
            'primary_key' => 'user_id'
        )
    );
}

So, in ZF, I should be able to call the orgs model and have an array of all of the users for that org; but, alas, no joy, just a NULL. Any help on this would be appreciated. :)

-Beau


Nanne Huiges Mon Oct 17 03:02:47 -0400 2011

Hmm, No quick insights. Could you post the errors?

Beau Beauchamp Mon Oct 17 06:42:32 -0400 2011

I also checked my linking table and found a bad UUID in the linking table. That was fixed but I'm still just getting a NULL returned. :(

When I run a simple SQL statement, it finds the user just fine:

SELECT users.* from users, orgs
INNER JOIN orgs_users ON (orgs_users.org_id = orgs.org_id)
WHERE orgs.org_id = '... uuid ...'
Beau Beauchamp Tue Oct 18 16:37:29 -0400 2011
Beau Beauchamp Wed Oct 19 05:40:02 -0400 2011

RESOLUTION: My problem was in how I was naming the classes. Watch your class names (singular and plural) and follow the examples carefully. :D

(1-4/4)