0

i am currently doing a complex multi-level classification system.

to simplify, i will explain what i am trying to accomplish by providing theoretical examples:

i have a group table:

group id| group name
   1    |     A
   2    |     B
   3    |     C

and i have a sub group table:

subgroup id| group id| sub-group name
    1      |    1    | A-aaaa
    2      |    1    | A-bbbb
    3      |    1    | A-cccc
    4      |    2    | B-111111
    5      |    2    | B-222222
    6      |    3    | C-ONE
    7      |    3    | C-TWO

and i have a model that uses these two tables as references:

class Model_ItemGrouping extends Model_Table {
   public $entity_code = 'items';

   function init() {
      parent::init();

      $this->addField('itemname')->caption('Item Description');

      $this->addField('group_id')->caption('Group Name')
         ->refModel('Model_Groups')->displayField('groupname');

      $this->addField('subgroup_id')->caption('Sub Group Name')
         ->refModel('Model_SubGroups')->displayField('subgroupname');
   }
}

now, i apply this model to a form on my code like this:

$form = $this->add('MVCForm');
$form->addSubmit();

$model=$form->setModel('ItemGrouping');

if($model->isInstanceLoaded())
   $form->getModel()->loadData(55); // force it to load item id #55

when this form is loaded and shown, the user can now edit and able to pick a different group and/or a different sub group for the item.

but what i want to do is LIMIT the set of options that can be selected from the drop down list depending on the current state of the item.

if item id #55 is currently from group 3, then the group should be locked to group 3 and the subgroup dropdown list should only contain subgroups of group 3 and not the whole list.

now, the more elaborate and specific question is this:

is there a way to DYNAMICALLY LOCK the two referenced models based on the state of the item:

Model_Groups reference by the field group_id

and

Model_SubGroups reference by the field subgroup_id

to only a particular set of selected items using the form's $model instance and its corresponding addCondition method in PHP and not Front-End JavaScript?

i imagine something like this (modifying my earlier snippet):

$form = $this->add('MVCForm');
$form->addSubmit();

$model=$form->setModel('ItemGrouping');

if($model->isInstanceLoaded()) {
   $form->getModel()->loadData(55); // force it to load item id #55

   // if item is of group #2, force references to show options for group #2
   $model->getFieldRefModel('group_id')->addCondition('id=',2);
   $model->getFieldRefModel('subgroup_id')->addCondition('group_id=',2);
}

NOTE: the getFieldRefModel is non-existent and this is of course over simplified just to provide an overview using static values, whereas i will also need to dynamically pass the item id number and examine first the current grouping state of the item before i actually set the filter conditions.

  • One note is to try to stick to newest ATK4 versions and avoid using obsolete methods. For example, MVCForm => Form, isInstanceLoaded => loaded, loadData => load etc. It doesn't matter in this particular case, but you can get in trouble later when upgrading ATK to some major new version. – DarkSide Apr 15 '13 at 23:24

1 Answers1

2

If I understood your requirements correctly, then instead of

$model->getFieldRefModel('group_id')->addCondition('id=',2);
$model->getFieldRefModel('subgroup_id')->addCondition('group_id=',2);

you can write

$model->ref('group_id')->addCondition('id',2);
$model->ref('subgroup_id')->addCondition('group_id',2);

That's with newest ATK4 version, which I guess you're not using :(

In fact, these conditions are added automatically behind the scenes, so you can simply write

$model->ref('group_id');
$model->ref('subgroup_id');

and don't forget to use hasOne, hasMany in your Models init method to define these fields. Right now these fields are not "relations", so ref() will not work while you don't define fields with hasOne/hasMany.

Also I believe you have a flaw in your data structure. What kind of relation you have between Item and SubGroup (1:n or n:1)?

DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • the Form->isInstanceLoaded() is a dead giveaway. yes, i am using the old 4.1.4 version of the toolkit due to server constraints (PHP 5.2). and the relation of group:subgroup is 1:n. the example i gave is for formulating this SO question only. and its much more complicated because the whole set of classification resides on one table which can have unlimited depth. 1:n:n:n:n... using only one table. – Open Technologist Apr 20 '13 at 16:07
  • I'm not very familiar with 4.1, so I hope Romans or somebody else can answer this question much better. – DarkSide Apr 22 '13 at 13:44