Hi
First a huge thank you to wbond and all of the contributors for this great library, I was and still am, very excited when I stumbled upon it.
I have a problem understanding why newly related records does not get saved. I have three class: Shop, Product, !ProductDescription.
!ProductDescription is essentially a join table between Shop and Product, thus shop_id and product_id is combined into the primary key of the product_descriptions table.
A row of Shop exists in the database. I'm trying to create new Products and !ProductDescriptions. When invoking store() on my Product object only the product gets stored. If I invoke store on my !ProductDescription object I get an error stating that shop_id and product_id is not set.
Here is the code:
$shop = new Shop(1);
$product = new Product();
$product->setProductCode('XFC-1001');
$product->setProductType('office');
$product->setBrand('Relief');
$product_description = new ProductDescription();
$product_description->setName('Torch');
$product_description->setShortDescription('short');
$product_description->setLongDescription('long');
$product->associateProductDescriptions($product_description);
$shop->associateProductDescriptions($product_description);
$product->store();
Passing TRUE to the store method seems to have no effect.
This is the DB scheme:
CREATE TABLE `product_descriptions` (
`product_id` int(11) unsigned NOT NULL,
`shop_id` int(11) unsigned NOT NULL,
`locale` varchar(255) NOT NULL DEFAULT 'da-DK',
`name` varchar(255) NOT NULL,
`short_description` varchar(255) NOT NULL,
`long_description` text,
`package_unit` varchar(255) NOT NULL DEFAULT 'stk',
PRIMARY KEY (`product_id`,`shop_id`),
KEY `shop_id` (`shop_id`),
CONSTRAINT `product_descriptions_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON DELETE CASCADE,
CONSTRAINT `product_descriptions_ibfk_2` FOREIGN KEY (`shop_id`) REFERENCES `shops` (`shop_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `shops` (
`shop_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`shop_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `products` (
`product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`product_code` varchar(255) NOT NULL,
`package_size` smallint(6) NOT NULL DEFAULT '1',
`brand` varchar(255) NOT NULL,
`original` tinyint(1) NOT NULL DEFAULT '0',
`product_type` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`),
KEY `product_code` (`product_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
I have looked through http://flourishlib.com/docs/fActiveRecord#RelatedRecordsOperations and the other ORM pages, but haven't found anything that could help me. These pages seems to have a bias on retrieving records.
I have a feeling my problem has to do with the fact that product_descriptions is a join table between products and shops, but it does not follow that suggest naming scheme: products_shops?
Any help will be most appreciated