4

I've got a decent understanding of the basics of eav and adding additional attributes to categories, products and other entities however I'd like to be able to form a one to many relationship between additional groups of objects for example:

  • Category -> FAQ's
  • Category -> Recommended Products
  • Category -> Blog Posts

Etc.

Could you get me pointed in the right direction?

What if I want to add an additional association with products to discern wither a product is recommended for the category?

  • Ok lets get a little more specific about the scenario. Lets say that we've already created a single table that represents FAQ's that has an id,question,answer. I want to then create a foreign key table that stores the associations between faq's and a category. Additionally if a category or faq is deleted all of the associations in the foreign key field are deleted. I would like to also have a tab within adminhtml/catalog/category that works in the same manner as the products tab. – ajameswolf Jul 1 '13 at 1:19
1

Your question is a bit general, there would be a lot of ways to do this and depending on the extensions you're using for FAQ and Blog posts there might be several best practices/ ways to do this.

To store related products, posts or faqs you could use 3 text attributes and store ID's of these objects comma separated. Basically adding 3 new attributes to the categories.

An other option is to create an extension that uses it's own database table to store the relations. This would probably have my preference because it allows for more scalability (adding sort order for example)

Both solutions will need an extension that adds new tabs to the category edit form from where you can pick the items you want to join to this category. Basically like the way you select products that belong to that category.

Also I suggest you add collection models to your extension for easy access to these collections you're building in the database and provide blocks through the extension that display this info in the frontend. Please keep the original category code and templates separated from these new functionalities you are making.

Maybe if you could give some more specific information on the direction you want to take in making this editable for the admin or how it should tie in to the frontend I can give a better awnser.

  • To begin with I would like to know how to handle the waterfall database modeling where if a particular category went away the FAQ's would also go away – ajameswolf Jul 11 '13 at 20:02
  • This could be done by adding a foreign key constraint to the FAQ items database (for example). This would be a reference to the primary key of the catalog_category_entity. By adding a ` ON DELETE CASCADE ON UPDATE CASCADE` to this foreign key deleting the category would result in deleting the FAQ item – Sander Mangel Jul 12 '13 at 13:19
  • I've described in the note below the initial question that I intended on creating a foreign key table that held the associations between the category entities and the questions EG two columns (category_id,question_id) where fk_table.question_id = qa_question_table.id and catalog_category_entity.id = fk – ajameswolf Jul 12 '13 at 14:36
  • Working from the perspective of needing to retreive the FAQ entities related to a given category , would you say it would be better to be able to do something like this Mage::registry('current_category')->getFaqs();//extend the core class Or to – ajameswolf Jul 12 '13 at 14:40
  • Or to instead either assume that a registry entry exists and call the Faq collection directly something like: $_faqs = Mage::getModel('faq/category')->getCategoryQuestions(); //within Namespace_Faq_Model_Category public function getCategoryQuestions($id){ if(empty($id)){ $id = Mage::registry('current_category')->getId(); } return $this->setCategoryById($id)->getCollection(); } – ajameswolf Jul 12 '13 at 15:16
0

Magento Categories are part of the Mage_Catalog core extension which describes a category as an associative collection of products. To re-use Categories for anything other than Products may introduce undesired effects.

What I believe you're describing is CMS Hierarchies. Hierarchies were introduced in Magento Enterprise CMS+. According to the Magento Enterprise documentation:

The CMS Hierarchy feature is a categorization tool for CMS pages, allowing users with no technical knowledge to create complex content pages and sections of content such as Articles, Knowledge Base, and more. The advantage to categorizing your CMS pages is that you will then be able to display automatically generated menus and/or pagination, to allow your customers to browse through the pages easily.

The question is - how would we make this feature available to Community Edition? Your best bet is to install an extension. One that seems to fit the bill (though I've never used it):

http://shop.bubblecode.net/magento-cms-tree.html

Another option is to build it yourself - it could be quite simple. All you would need is a simple two-column join table that associates IDs of Hierarchy nodes to CMS page IDs. Another table would describe hierarchy node information - name, creation date, etc.

Two models and a collection would be all that is needed to get the information in and out of the db - a model to fetch and save information for the afforementioned hierarchy nodes themselves, and a model to fetch/save for the joins.

Displaying the information in Magento's admin section would be a deeper dive into how the Mage_Adminhtml module works and includes setting up admin forms, routes, controllers, and grids.

Best of luck!

0

You can take a look at this answer: https://magento.stackexchange.com/a/14842/146.
It contains the code for creating a relation between categories and a custom entity (other than products).
It's a lot of code and there is no point in having it replicated in here.
The main idea is to add a new tab on your entity edit page similar to the one the product has, and one tab with your custom entities on the category add/edit page.
Of course you need a table to store the relations.
You will find everything in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.