⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Set relation with one table



Bobby Stegwee
  • profile picture
  • Member

Posted 08 August 2012 - 08:41 AM

I was testing with GC and was testing with relations, between different tables this works great but is this also possible with only one table.

The table contains the following fields.[list]
[*]category_id
[*]category_name
[*]category_parent_id
[/list]
I want to create a form where I can add a category and select another earlier added category as a parent. Is this possible? If so, can anybody tell me how?

Nage
  • profile picture
  • Member

Posted 09 August 2012 - 20:19 PM

Hi Bobeski!

It seems to work for me, even with multiple relations:


$crud = new grocery_CRUD();

$crud->set_table('poll_categories');

$crud->set_relation('parent_pollcategoryid','poll_categories','pollcategoryname');
$crud->set_relation('userid','users','username');

$output = $crud->render();


Database:

CREATE TABLE `poll_categories` (
`pollcategoryid` int(11) NOT NULL AUTO_INCREMENT,
`pollcategoryname` varchar(255) NOT NULL DEFAULT '',
`isprivate` tinyint(1) NOT NULL,
`parent_pollcategoryid` int(11) DEFAULT NULL,
`sortorder` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`date_entry` datetime NOT NULL,
`date_updated` datetime DEFAULT NULL,
PRIMARY KEY (`pollcategoryid`),
KEY `key_parent_pollcategoryid` (`parent_pollcategoryid`),
KEY `key_userid` (`userid`),
CONSTRAINT `fk_poll_categories_parent_pollcategoryid` FOREIGN KEY (`parent_pollcategoryid`) REFERENCES `poll_categories` (`pollcategoryid`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_poll_categories_userid` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3000820 DEFAULT CHARSET=utf8;


Proof:
[attachment=244:Screen Shot 2012-08-09 at 22.10.10.png]

[attachment=245:Screen Shot 2012-08-09 at 22.09.42.png]

Maybe there's something wrong with your code or database definition. Could you post your code and database?

Greetings,

Nage