This is exactly what I also want to know. Is this even possible with php.activerecord?
I also just discovered that there's no documentation for this. But, you can see here (http://www.phpactiverecord.org/docs/ActiveRecord/HasAndBelongsToMany) that it's not even implemented.
Am I missing something? Does this software seriously not have a way to support many-to-many relationships?
kla -- It is really not good that this question has gone unanswered for 3 days. This is not some bleeding edge feature.
I got it to work with the help of this thread:
http://www.phpactiverecord.org/boards/4/topics/99-many-to-many-relationship
But something like $many_to_my would be nicer ;)
Please use has_many through - this means your "join/through table" will need to also have a model. The only benefit has_and_belongs_to_many would have over has_many through is that habtm does not require a model for the "join" table.
I can't get has_many through to work.
class WmVideo extends ActiveRecord\Model { static $belongs_to = array( array('country') ); static $has_many = array( array('wm_video_regions', 'foreign_key' => 'wm_video_id'), array('wm_regions', 'through' => 'wm_video_regions') ); }
(By the way, why should I have to declare the foreign key? If I don't it looks for wmvideo_id, which seems incorrect since the table is called wm_videos and it finds that correctly)
Anyway, the error I get when trying to access wm_regions is this:
Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association wm_video_regions in model WmVideo' in /var/www/shared/test/lib/php-activerecord/lib/Relationship.php:464 Stack trace: #0 /var/www/shared/test/lib/php-activerecord/lib/Model.php(493): ActiveRecord\HasMany->load(Object(WmVideo)) #1 /var/www/shared/test/lib/php-activerecord/lib/Model.php(386): ActiveRecord\Model->read_attribute('wm_regions') #2 /var/www/crcna.org/datatest/html/wm_videos/admin/index.php(22): ActiveRecord\Model->__get('wm_regions') #3 {main} thrown in /var/www/shared/test/lib/php-activerecord/lib/Relationship.php on line 464
Strange that it says that it can't find that association, when I've just declared it a line above.
See this issue where I've posted a fix:
https://github.com/kla/php-activerecord/issues#issue/68
Though has_many through doesn't seem to be fully supported yet as you can't do eager loading or joins:
https://github.com/kla/php-activerecord/issues#issue/87
(1-6/6)
Subject: associations for lookup tables
I'm having trouble wrapping my head around how to do associations for lookup tables. For instance, if I have the tables: item, category, and item_category_lookup, so that items can have multiple categories, how would I set up my associations?