Self-Referencing Relationships

edited August 2013 in Modules
I"m trying to figure out if any of the built-in relationships can be used to implement a self-referencing one-to-many relationship.

I have "Projects" which belong to "Customers" (Projects Module will have foriegn_key => customers_module).

Now a Project can have "related projects", which would be a one-to-many relationship back to it's own table. This is not a "foreign key" by definition, but foreign_key is the only relationship that represents a one-to-many. [side note: I have a tough time remembering that 'has_many' is a many-to-many relationship. In the above example I would naturally think "a customer has many projects"]

I haven't started building my modules yet, still drawing out the database relationships, but I'm thinking about the Model definitions as part of the modeling. Can I use something like

class Projects_model extends Base_module_model { public $foreign_keys = array('id' => array(FUEL_FOLDER => 'projects_model'))); function __construct() { parent::__construct('projects'); } }

Comments

  • edited 5:34AM
    Were you planning on storing those related projects in their own table to lookup? If so, you should be able to use has_many to do this and it would store it in the fuel_relationships table. The blog module uses this to relate posts to other posts.
    public $has_many = array('related_projects' =>'projects_model');
  • edited 5:34AM
    Just another nod for this approach, I used it to relate products to other products (and other things), EG:

    public $has_many = array( 'products' => array( 'model' => array(STORE_FOLDER => 'products_model') ), 'categories' => array( 'model' => array(STORE_FOLDER => 'categories_model') ), 'brands' => array( 'model' => array(STORE_FOLDER => 'brands_model') ), 'manufacturers' => array( 'model' => array(STORE_FOLDER => 'manufacturers_model') ) );
Sign In or Register to comment.