Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Naming relations in self referential table

posted by mblarsen 9 years ago

Hi Could someone point out to me how to properly name the relations in the situation when an ActiveRecord is self-referential. Like this:

A Category can have many sub categories:

CREATE TABLE `categories` (
  `category_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) unsigned DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `ordinal` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`category_id`),
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `categories_ibfk_2` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`category_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Buy default you would use the ActiveRecord like this to get parent:

$category->createCategory();

and to get children:

$category->buildCategories();

I want to use parent and children instead, so I have set up the configure() method like this:

fGrammar::addHumanizeRule('Child', 'children');
fORMRelated::overrideRelatedRecordName($this, 'Category', 'Child', 'categories');
fORMRelated::overrideRelatedRecordName($this, 'Category', 'Parent', 'category');

But that seems to have no positive effect. The second line produces this exception:

The route specified, categories, is not a valid route between categories and categories. Must be one of: parent_id.

I set the humanize rule because I'm unsure that flourish will correctly humanize the relation Child using pluralization.

How do I figure out the correct route?

fORMRelated::overrideRelatedRecordName() only changes the name of the related record when referring to them in validation messages. In this case, if you took a category and called $category->populateCategories() and then $category->validate(), the overridden record name would be used when referring to validation error in the children.

It sounds to me like you want to change the method names you are using. In this case you just want to manually override them:

class Category extends fActiveRecord
{
    public function createParent()
    {
        $this->createCategory();
    }

    public function buildChildren()
    {
        $this->buildCategories();
    }
}
posted by wbond 9 years ago