Generic approach for 1-N relation as extension

First of all, I’m back after time without posting… I have been working hard on another Java/OSGI project that consumed my whole person, but now I want to develop an idea I had time ago…

AIM: Ok, I’m a maniac of modularization and I want to develop extensions/modules that contribute new models that can be appended to the main application AS SIMPLY AS I can.

Example: my main app works with Clients, Providers, etc and I want to develop an extension to add functionality for 1-N relations like Comments, Files, Images, etc.

What I have done until now:

  • Create a table for weak part with id, entity, EXid, (rest of the fields)… where entity is the class name of the strong part, for example ‘Client’ or ‘Provider’, and EXid is the foreign key (ex. 256)

  • Create the weak model (Comment) with (if needed) autocreate table code in tableName method:


    

public function tableName() {

    // Auto create table if it's not created

    $conn = Yii::app()->db;

    if ($conn->createCommand("SHOW TABLES LIKE '".self::TABLE_NAME."'")->query()->rowCount <= 0) {

        $sql = file_get_contents(dirname(__FILE__)."/schema.sql");

        $conn->createCommand($sql)->execute();

    }

    return self::TABLE_NAME;

}



  • Create a Behavior (like CommentBehavior) that:

[list]

  • Adds the 1-N relation to the model attached in attach/detach methods

  • Do comments validation in beforeValidate() method

  • Do saving in afterSave()

  • Do deletion of the comments in beforeDelete()

[*]Create partial templates for comment rendering and for comment forms (or files, or images, or phones, etc)

[/list]

Ok, what I get with this?: if I need comments for a new entity I need simply:

  • Add the behavior to the model of such entity

  • Append to the _form template a partial render of the form of the weak entity

  • Append to the _view tempalte a partial render of the view of the weak entity

Of course, if you want to see all this in action i can post code, but what I want is feedback about if it’s ok for you all, someone has an idea to improve this, etc.