如何限制 ATK4 中引用模型中的列表
我有一个名为 Task 的模型,其定义如下(与问题无关的字段已删除),
<?php
class Model_Task extends Model_Table {
public $entity_code='vscrum_task';
public $table_alias='tk';
function init(){
parent::init();
// debug causes error in Ajax in ATK v4.1.1
// $this->debug(true);
$this->addField('id')->system(true)->visible(false);
$this->addField('task_desc')->mandatory(true)->visible(true);
$this->addField('tasktype_id')->mandatory(true)->refModel('Model_TaskType');
$this->addField('team_id')->system(true)->visible(false);
并且 refModel 任务类型的定义如下(与问题无关的字段已删除)
<?php
class Model_TaskType extends Model_Table {
public $entity_code='vscrum_tasktype';
public $table_alias='ty';
function init(){
parent::init();
$this->addField('id')->mandatory(true);
$this->addField('name')->mandatory(true);
$this->addField('team_id');
}
}
我有一个基于任务的 CRUD,现在是(感谢来自stackoverflow 上的 Jancha 和 Romans)工作正常。
我想将任务类型下拉列表中的选项限制为仅为用户团队定义的任务类型。我尝试在任务类型模型中放置一个 addCondition ,引用我之前记住的会话变量
$this->addCondition('team_id',$p->api->recall('team_id'));
,并且还使用直接调用登录使用的值
$this->addCondition('team_id',$p->api->auth->get('team_id'));
,但这会导致在网格中显示任务类型良好
但在 Ajax 对话框中将其保留为空以进行编辑和添加。
如果我从 TaskType 模型中删除 addCondition 行,它会显示列表中的所有值,但我将始终希望这仅限于一个子集。
由于这是引用的模型而不是 CRUD 所基于的模型,因此关于如何使其按预期工作有什么建议吗?
我尝试了Roman的建议,即拥有一个TaskType模型和一个从TaskType_Team扩展而来的新模型,其中包含addCondition,就像此类
Model_TaskType_Team扩展Model_TaskType { 函数初始化(){ 父级::init();
$this->addCondition('team_id',$p->api->auth->get('team_id'));
}
为此,我需要创建一个名为 TaskType 的子目录 undel Model,否则它找不到新模型,但最终结果是相同的。我认为这与我之前遇到的另一个问题有关,其中 Ajax 对话框无法访问 $p->api,因此不会显示限制(这就是为什么它对于同一页面上的网格工作正常,而不是在ajax 对话框,但我不想使用 StickyGet 来解决此问题以确保安全(不希望能够修改 URL 以查看其他团队数据)和会话变量($p->auth->memorise 和$p->auth->recall) 在这种情况下似乎也不起作用 - 还有进一步的建议吗?
I have a model called Task defined like this (fields not relevent to question removed)
<?php
class Model_Task extends Model_Table {
public $entity_code='vscrum_task';
public $table_alias='tk';
function init(){
parent::init();
// debug causes error in Ajax in ATK v4.1.1
// $this->debug(true);
$this->addField('id')->system(true)->visible(false);
$this->addField('task_desc')->mandatory(true)->visible(true);
$this->addField('tasktype_id')->mandatory(true)->refModel('Model_TaskType');
$this->addField('team_id')->system(true)->visible(false);
and the refModel tasktype is defined like this (fields not relevent to question removed)
<?php
class Model_TaskType extends Model_Table {
public $entity_code='vscrum_tasktype';
public $table_alias='ty';
function init(){
parent::init();
$this->addField('id')->mandatory(true);
$this->addField('name')->mandatory(true);
$this->addField('team_id');
}
}
I have a CRUD which is based on task and is now (thanks to help from Jancha and Romans on stackoverflow) is working fine.
I want to limit the options in the drop down for TaskType to only those tasktypes defined for the user's team. I tried putting an addCondition in the TaskType Model referencing a session variable i had previously memorized
$this->addCondition('team_id',$p->api->recall('team_id'));
and also using a direct call to a value for the logged in use
$this->addCondition('team_id',$p->api->auth->get('team_id'));
but this results in showing the Tasktype fine in the Grid
but leaves it empty for both Edit and Add in the Ajax dialog.
If i remove the addCondition line from the TaskType Model, it shows all values in the list but i will always want this restricted to a subset.
As this is the referred Model and not the Model that the CRUD is based on, any suggestions on how i get this to work as expected ?
I tried Roman's suggestion of having a model which is the TaskType and a new model extended from that which is the TaskType_Team with the addCondition in it like this
class Model_TaskType_Team extends Model_TaskType {
function init(){
parent::init();
$this->addCondition('team_id',$p->api->auth->get('team_id'));
}
for which i needed to create a subdirectory undel Model called TaskType otherwise it didnt find the new Model but the end result is the same. I think this is related to another issue i previously had where the Ajax dialog loses access to $p->api and so doesnt display the restriction (and this is why it works fine for the grid on the same page as that isnt in an ajax dialog but i dont want to use a stickyGet to resolve this for security (dont want to be able to modify the URL to see other teams data) and session variables ($p->auth->memorise and $p->auth->recall) also dont seem work in this case - any further suggestions ?
请记住,您可以像这样扩展您的模型。事实上,这在较大的项目中经常使用。
Remember that you can extend your models like that. In fact, this is very often used in larger projects.