Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL & PHP - Creating Multiple Parent Child Relations

I'm trying to build a navigation system using categories table with hierarchies. Normally, the table would be defined as follows:

id (int) - Primary key
name (varchar) - Name of the Category
parentid (int) - Parent ID of this Category referenced to same table (Self Join)

But the catch is that I require that a category can be child to multiple parent categories.. Just like a Has and Belongs to Many (HABTM) relation.

I know that if there are two tables, categories & items, we use a join table categories_items to list the HABTM relations. But here i'm not having two tables but only table but should somehow show HABTM relations to itself. Is this be possible using a single table? If yes, How? If not possible, what rules (table naming, fields) should I follow while creating the additional join table?

I'm trying to achieve this using CakePHP, If someone can provide CakePHP solution for this problem, that would be awesome. Even if that's not possible, any solution about creating join table is appreciated. Thanks for your time.

-- Edit -- My question seems to be a bit confusing, so I'm trying to restate what I'm looking for. In traditional self referenced (self join) parent-child relations, each item can have only one parent. What I'm looking for is to simulate a HABTM relation i.e. multiple parents for each item.

Categories & Items - To define HABTM, we use categories_items join table.

If within Categories I need HABTM, what should I do?

like image 732
Ashok Avatar asked Mar 25 '10 10:03

Ashok


People also ask

What is MySQL is used for?

MySQL is a database management system. It may be anything from a simple shopping list to a picture gallery or the vast amounts of information in a corporate network. To add, access, and process data stored in a computer database, you need a database management system such as MySQL Server.

What is difference between SQL and MySQL?

SQL is primarily used to query and operate database systems. MySQL allows you to handle, store, modify and delete data and store data in an organized way. SQL does not support any connector. MySQL comes with an in-built tool known as MySQL Workbench that facilitates creating, designing, and building databases.

Is MySQL better than Microsoft SQL?

Both SQL Server and MySQL can run high-performance workloads, although third-party independent testing suggests that SQL Server is the winner here. Both SQL Server and MySQL include feature-rich client applications (SSMS and MySQL Workbench, respectively).

Is MySQL still free?

MySQL is an open source project. That is, the complete source code of MySQL is freely available. Since June 2000 (that is, since version 3.23. 19) the GNU Public License (GPL) has been valid for MySQL.


1 Answers

I hope it isn't bad form to answer a second time, having misunderstood the question the first time. The following is essentially a CakePHP implementation of pinkgothic's answer.

New HABTM join table:

CREATE TABLE `categories_parent_categories` (
  `category_id` int(10) unsigned NOT NULL,
  `parent_category_id` int(10) unsigned default NULL,
  `order` int(10) unsigned NOT NULL default '0'
);

Association in model:

class Category extends AppModel
{
    var $hasAndBelongsToMany = array(
        'ParentCategory' => array(
            'className'             => 'Category',
            'joinTable'             => 'categories_parent_categories',
            'foreignKey'            => 'category_id',
            'associationForeignKey' => 'parent_category_id',
            'order'                 => 'CategoriesParentCategory.order'
        )
    );
}
like image 170
Daniel Wright Avatar answered Oct 02 '22 06:10

Daniel Wright