I’m having a hard time with this CakePHP model/controller development.
I have a Product model. The model hasMany pricing, images, videos, content tabs etc.
The model also needs to have many relationships to other products. – eg. accessories, kits, sub-products (variant models of a parent)
So I had created a products_products table where i have a product_id and a related_id – the id of the related accessory/kit or sub product – plus the relationship type.
Table looks like: id,product_id,related_id,type,sort…
My challenge is this – when I load a main product, I want to get all the associated model data for the product which it is related – so, treat the related model like it’s it’s own product (which it is) and load all associated model data
I can load data from the related table easy enough using HABTM relationship .. but simply knowing that the product is related doesn’t give me it’s product model data – pricing, videos, images, promotions, etc etc .
So what’s the best way to develop and load this model? can I do it with standard associated model relationships or do I need to use the main product model to get to my related items, using a list of product ids from the related products_products table using a subquery (which seems very un-Cake like)
? I’d appreciate any input from experienced cake guys.
Thanks in advance.
You can save yourself a lot of headaches by using self associations.
Essentially, you’ll need to create a parent_id field in your Product model, and products with accessories and the like will have the id of the main product in the parent_id field. This will simplify getting the products out the database as you can just call find(‘threaded’).
From there, it would be easy to set up hasMany relationships for tables holding video/image data about a product.