在 ATK4 中,我如何修改模型以获得 DISTINCT 列表

发布于 2024-12-13 05:27:10 字数 2024 浏览 1 评论 0原文

我定义了一个故事列表,并在另一个表中定义了附加到这些故事的一组任务。

对于给定的用户,我想获得一个不同的故事编号列表,例如,如果用户有多个任务,则故事只会列出一次。

我可以使用直接 SQL 来完成此操作,但我应该如何使用 ATK4 中的模型来实现此操作?

目前,将成员链接到故事的表具有如下定义的模型:

<?php
 class Model_StoryMember extends Model_Table {
public $entity_code='vscrum_story_member';
public $table_alias='sm';

function init(){
           parent::init();
           $this->addField('story_id')->system(true)->visible(false)->   
             defaultValue($this->api->recall('story_id'));
           $this->addField('team_id')->system(true)->visible(false)->
             defaultValue($this->api->recall('team_id'));
           $this->addField('sprint_id')->system(true)->visible(false)->
             defaultValue($this->api->recall('sprint_id'));
           $this->addField('member_id')->refModel('Model_Member');
           $this->addField('points')->datatype('numeric');
           $this->addField('notes')->datatype('text');
 }
 }

该模型可以包含每个用户的记录,例如,

    STORY
    ID     Name     Points Responsible
    1000   Story 1      2     1
    1001   Story 2      3     1

    STORY_MEMBER
    ID     Story    Member  Points Description
    1      1000     1       .5     Task 1
    2      1000     1       .5     Task 2 
    2      1000     2       1      Task 1
    3      1001     1       1      Task 1
    4      1001     2       1      Task 2 
    5      1001     3       1      Task 3

因此,如果我查看用户 1 或用户 2 的数据,我只希望返回两行(即使用户一故事1000中有两个任务) 故事1000 故事 1001

对于用户 3,我只会得到一行 故事 1001。

要进一步添加,我希望能够合并两个查询并返回不同的列表,因此如果我查询成员 1, 如果我有一个基本的 SQL,例如,

SELECT id, name
  from story
 where responsible=1
 union
select s.id, s.name 
  from story s, story_member sm
 where s.id=sm.story_id
   and sm.member_id=1

我将获得一个成员负责的故事列表以及他们所涉及的故事列表。工会负责唯一的排序,因此每个故事仅返回一次(因为他们很可能参与他们拥有的故事),但也可能存在他们参与但其他人负责的故事。

有没有一种方法可以将两个查询合并在一起,以便我在页面中返回一个数组?

TIA

I have a list of stories defined and, in another table, a set of tasks attached to those stories.

For a given user, i want to get a distinct list of story numbers e.g. if the user has multiple tasks, the story will only be listed once.

I can do it with a direct SQL but how should i implement this with a model in ATK4 ?

At the moment, the table linking members to stories has a model defined like this

<?php
 class Model_StoryMember extends Model_Table {
public $entity_code='vscrum_story_member';
public $table_alias='sm';

function init(){
           parent::init();
           $this->addField('story_id')->system(true)->visible(false)->   
             defaultValue($this->api->recall('story_id'));
           $this->addField('team_id')->system(true)->visible(false)->
             defaultValue($this->api->recall('team_id'));
           $this->addField('sprint_id')->system(true)->visible(false)->
             defaultValue($this->api->recall('sprint_id'));
           $this->addField('member_id')->refModel('Model_Member');
           $this->addField('points')->datatype('numeric');
           $this->addField('notes')->datatype('text');
 }
 }

This model could contain a record for each user e.g.

    STORY
    ID     Name     Points Responsible
    1000   Story 1      2     1
    1001   Story 2      3     1

    STORY_MEMBER
    ID     Story    Member  Points Description
    1      1000     1       .5     Task 1
    2      1000     1       .5     Task 2 
    2      1000     2       1      Task 1
    3      1001     1       1      Task 1
    4      1001     2       1      Task 2 
    5      1001     3       1      Task 3

so if i view the data for user 1 or user 2, i only want two lines returned (even though user one has two tasks in story 1000)
story 1000
story 1001

and for user 3, i would only get one row for
story 1001.

To add further, i want to be able to union two queries and return the distinct list so if i query it for member 1,
if i have a basic sql e.g.

SELECT id, name
  from story
 where responsible=1
 union
select s.id, s.name 
  from story s, story_member sm
 where s.id=sm.story_id
   and sm.member_id=1

i will get a list of the stories which member one is responsible for and also those for which they are involved. The union takes care of the sort unique so each story is only returned once (as it's very likely they are involved in the stories they own) but there may also be stories they are involved in but someone else is responsible for.

Is there a way to union the two queries together so i get one array returned in the page ?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-12-20 05:27:10

您最好手动使用查询。尝试:

$dsql->option('distinct');

您可以更改视图的查询,例如,如果您有网格,则:

$this->grid->dq->option('distinct');

在设置模型后调用此查询。

You are probably better off using query manually. Try:

$dsql->option('distinct');

You can change the query of your view, for example if you have a grid, then:

$this->grid->dq->option('distinct');

Call this AFTER the model has been set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文