Gabriel Littman Thu Aug 19 19:04:36 -0400 2010

Subject: through => through

Hi,

This is great stuff. But I think I'm trying to push it too far...

Do the relations support relationships through multiple tables....

I.E.

publisher -> has_many publications
publication -> belongs_to publisher
publication -> has_many advertisements
advertisement -> has_many publications
advertisement -> belongs_to user
user -> has_many advertisements (works)
user -> has_many publications, through => advertisements (works)
user -> has_many publishers, through => publications (fails)

Is this kind of thing supported by php-active-record?


David Wattier Fri Aug 20 05:01:00 -0400 2010

Hi,

What happens if you add publication -> belongs_to user ? :p

Gabriel Littman Fri Aug 20 14:22:39 -0400 2010

I believe I did try that but still couldn't get it...

I'll try it again report back.

EDIT: Even with all the relationships setup I couldn't get it to work....

Gabriel Littman Thu Aug 26 21:47:43 -0400 2010

Here's what I'm actually working with...

// [ campaigns ]
// [ int id ]

class Campaign extends ActiveRecord\Model { 
    static $has_many = array(
        array('campaign_publications'),
        array('publications', 'through' => 'campaign_publications'),
        array('publishers', 'through' => 'publications')  //fails
    );
}

// [ campaigns_publications ]
// [ int id ]              
// [ int campaign_id ]
// [ int publication_id ]

class CampaignPublication extends ActiveRecord\Model {
    static $table_name = 'campaigns_publications';
    static $belongs_to = array(
        array('campaign'), 
        array('publication')
    );
    static $has_one = array(
        array('publisher', 'through' => 'publication') //fails
    );     
}

// [ publications ]
// [ int id ]              
// [ int publisher_id ] 
class Publication extends ActiveRecord\Model {
    static $has_many = array(
        array('campaign_publications'),
        array('campaigns', 'through' => 'campaign_publications'),
    );

    static $belongs_to = array(
        array('publisher'),
    ); 
}

// [ publishers ]
// [ int id ]              
class Publisher extends ActiveRecord\Model {
    static $has_many = array(
        array('publications'),
        array('campaign_publications', 'through' => 'publications'),  //fails
        array('campaigns', 'through' => 'campaign_publications')  //fails
    );
}

Kien La Mon Aug 30 18:57:08 -0400 2010

Could you show your User class with the relevant parts?

Gabriel Littman Wed Sep 08 15:15:30 -0400 2010

The original post was kind of a generalization of what I was trying to do (an attempt to make it more clear) and not actually what I was working with. Code that I posted (post 4 I think) is actual code I'm working with that has been cut down to relevant sections. Basically in post 1 and 4 campaign and advertisement are synonymous.

Here's the relevant user Model and Campaign update


// [ campaigns ]
// [ int id ]     
// [ int user_id ]
class Campaign extends ActiveRecord\Model { 
    static $belongs_to = array(
        array('user')
    );
    static $has_many = array(
        array('campaign_publications'),
        array('publications', 'through' => 'campaign_publications'),
        array('publishers', 'through' => 'publications')  //fails
    );
} 

// [ users ]
// [ int id ]
class User extends ActiveRecord\Model {       
    static $has_many = array(
        array('campaigns'),
        array('publications', 'through' => 'campaigns'), //fails
        array('publishers', 'through' => 'publications') //fails
    );       
}

Notice that I've marked relationships that are failing in the code both here and above. They all amount to the same type of use case that isn't really working.

I'm assuming that this not a supported use case. Let me know if I'm just using it wrong, there's a way to get similar functionality, or if this functionality is coming soon or in experimental version.

(1-5/5)