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.