问题
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.
回答1:
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)?
来源:https://stackoverflow.com/questions/15994508/how-to-set-a-filter-on-a-field-referencing-a-model-in-atk4