[eluser]mrtopher[/eluser]
Have been struggling with advanced relationship for a couple days and am not getting anywhere.
I have two tables with a simple one-to-many relationship between them.
Code:
CREATE TABLE IF NOT EXISTS `brands` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`company_id` int(10) unsigned DEFAULT NULL,
`brand_category_id` int(10) unsigned DEFAULT NULL,
`name` varchar(200) NOT NULL,
`website` varchar(255) DEFAULT NULL,
`logo_image` varchar(100) NOT NULL,
`sef_url` varchar(200) NOT NULL,
`date_added` datetime NOT NULL,
`date_updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `brand_categories`
--
CREATE TABLE IF NOT EXISTS `brand_categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`sef_url` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Brand has one category and a brand category has many brands. To my understanding this doesn't need a join table because it isn't a many-to-many relationship, although I know that's an option.
I setup my two models as follows:
Code:
class brand_category extends DataMapper
{
var $has_many = array('brands' => array(
'class' => 'brand',
'other_field' => 'category',
'join_other_as' => 'brand',
'join_self_as' => 'brand_category'
)
);
}
class brand extends DataMapper
{
var $has_one = array(
'category' => array(
'class' => 'brand_category',
'other_field' => 'brands',
'join_other_as' => 'brand_category',
'join_self_as' => 'brand'
),
'company'
);
//var $has_many = array('event', 'promotion', 'user');
}
Everything looks fine until I try to save the relationship (a brand with a related category).
Code:
$cat = new Brand_category();
$cat->get_by_id(1);
$brand->name = 'Test';
$brand->website = 'http://google.com';
$brand->logo_image = 'http://dummyimage.com/150x100/000/fff.png&text;="Test+logo"';
$brand->sef_url = 'test';
$brand->save($cat, 'category');
I get an error "Unable to relate brand with brand_category." I have tried using just save() (as shown above) and also save_category() which I believe should work according to the documentation. Nothing has worked.
While trying to troubleshoot the issue it's looking like the $related_field is not getting passed to save() for whatever reason. When I placed breakpoints in the save() function of datamapper.php (line 1568) it wasn't seeing that I was passing category as a second argument (which solves the issue when I hardcode it into datamapper.php).
Am I doing something wrong? Any help would be greatly appreciated.