Jessica Posted February 9, 2011 Share Posted February 9, 2011 Has anyone here used the ADOdb Active Record implementation? http://phplens.com/lens/adodb/docs-active-record.htm I'm trying to use the ClassHasMany option to automatically load children. But it's not working. I have ClassBelongsTo on another class and it works fine, but this one is not working. I think the problem might lie with the primary key - foreign key relationships. My parent table: CREATE TABLE IF NOT EXISTS `grids` ( `grid_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `location_id` int(11) NOT NULL, `city_id` int(10) unsigned DEFAULT NULL, `nation_id` int(10) unsigned DEFAULT NULL, `continent_id` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`grid_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `grids` -- INSERT INTO `grids` (`grid_id`, `x`, `y`, `location_id`, `city_id`, `nation_id`, `continent_id`) VALUES (1, 10, 10, 1, 1, 1, 1), (2, 10, 9, 1, 1, 1, 1); The child table CREATE TABLE IF NOT EXISTS `beasts` ( `beast_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `beast_type_id` int(10) unsigned NOT NULL, `max_hp` int(10) unsigned NOT NULL, `current_hp` int(10) unsigned NOT NULL, `grid_id` int(10) unsigned NOT NULL, PRIMARY KEY (`beast_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `beasts` -- INSERT INTO `beasts` (`beast_id`, `beast_type_id`, `max_hp`, `current_hp`, `grid_id`) VALUES (1, 1, 10, 10, 1), (2, 2, 5, 5, 1), (3, 1, 10, 10, 2), (4, 2, 5, 5, 2); The Class definitions <?php Class Grid Extends ADOdb_Active_Record{ } //This line is commented out because it didn't seem to help the problem, but I have tried it with it uncommented. I have also tried with only this one and not the Class one. //ADODB_Active_Record::TableKeyHasMany('Grids', 'grid_id', 'Beasts', 'grid_id') ADODB_Active_Record::ClassHasMany('Grid', 'Beast','grid_id'); Class Beast Extends ADOdb_Active_Record{ function __toString(){ return $this->Beast_Type->beast_type; } } //This one actually works. If I load a beast, the beast type does show up. ADODB_Active_Record::ClassBelongsTo('Beast','Beast_Type','beast_type_id'); ?> The php code I run <?php $grid = new Grid(); $grid->load('grid_id=?', 1); var_dump($grid); var_dump($grid->Beast); $beast = new Beast(); I've tried $grid->Beast, ->Beasts, ->beast, and ->beasts. Nothing is in any of them. $grid does have the right data: object(Grid)[40] public '_dbat' => int 0 public '_table' => string 'Grids' (length=5) public '_tableat' => string 'Grids' (length=5) public '_where' => string 'grid_id=?' (length=9) public '_saved' => boolean true public '_lasterr' => boolean false public '_original' => array 0 => string '1' (length=1) 1 => string '10' (length=2) 2 => string '10' (length=2) 3 => string '1' (length=1) 4 => string '1' (length=1) 5 => string '1' (length=1) 6 => string '1' (length=1) public 'foreignName' => string 'grid' (length=4) public 'grid_id' => string '1' (length=1) public 'x' => string '10' (length=2) public 'y' => string '10' (length=2) public 'location_id' => string '1' (length=1) public 'city_id' => string '1' (length=1) public 'nation_id' => string '1' (length=1) public 'continent_id' => string '1' (length=1) array empty Help? Thanks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Author Share Posted February 9, 2011 Well, it looks like now the Beast_Type part isn't working either. So...something is wrong with these both. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Author Share Posted February 9, 2011 Okay, I got the beast to beast type (belongs to) part working again. I think it did have to do with the ids not being "id". This seems to have solved it. ADODB_Active_Record::TableKeyBelongsTo('beasts', 'beast_id', 'beast_types', 'beast_type_id', 'beast_type_id'); ADODB_Active_Record::ClassBelongsTo('Beast','Beast_Type','beast_type_id','beast_type_id'); The order seems to be important. Hopefully if anyone else runs into this trouble, this will help you solve it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.