簡體   English   中英

TYPO3 10 Exbase雙向m:n關系排序

[英]TYPO3 10 Exbase bidirectional m:n relation sorting

在我的擴展中,我有兩個模型團隊成員(父母)和專家(孩子)。 兩個模型之間的雙向關系存儲在中間表中。 這工作正常,但有一個問題。

當我在 TYPO3 后台編輯團隊成員記錄並為其分配專業記錄,然后打開相應的專業記錄時,新分配的團隊成員記錄顯示在列表頂部而不是末尾。 反過來也會出現問題。

這是因為在保存記錄時,中間表中的排序分別 sorting_foreign 字段被設置為其默認值 (0)。

我怎樣才能解決這個問題?

TCA model 團隊成員

...
'expertise' => [
    'label' => 'Expertise',
    'config' => [
        'type' => 'select',
        'renderType' => 'selectMultipleSideBySide',
        'foreign_table' => 'tx_btp_domain_model_expertise',
        'foreign_table_where' => 'ORDER BY tx_btp_domain_model_expertise.title',
        'MM' => 'tx_btp_team_expertise_mm',
        'maxitems' => 50,
    ],
],
...

TCA model 專長

...
'team' => [
    'label' => 'Team',
    'config' => [
        'type' => 'select',
        'renderType' => 'selectMultipleSideBySide',
        'foreign_table' => 'tx_btp_domain_model_team',
        'MM' => 'tx_btp_team_expertise_mm',
        'MM_opposite_field' => 'expertise',
        'maxitems' => 50,
    ],
],
...

中間表定義:

CREATE TABLE tx_btp_team_expertise_mm (
    uid_local int(11) unsigned DEFAULT '0' NOT NULL,
    uid_foreign int(11) unsigned DEFAULT '0' NOT NULL,
    sorting int(11) unsigned DEFAULT '0' NOT NULL,
    sorting_foreign int(11) unsigned DEFAULT '0' NOT NULL,

    KEY uid_local (uid_local),
    KEY uid_foreign (uid_foreign)
);

在數據庫級別,您可以做一些事情,我在下面鏈接的解決方案是根據現有值增加值,因此不考慮包含的頁面或記錄,這將導致比值與單頁相關時更高的排序值只有: https://stackoverflow.com/a/3292500/1019850

同一頁面上的另一個解決方案是使用觸發器,這可能是一個好方法(閱讀我在那里的評論以調整過時的代碼): https://stackoverflow.com/a/3324116/1019850

這些代碼示例中的任何一個都可以作為根據自己的需要調整命題並對其進行優化的基礎。

在 TYPO3 級別上,您可以包含一個 function itemsProcFunc ,它可以包含檢索現有值的邏輯並僅根據需要增加新值: https://docs.typo3.org/m/typo3/reference-tca/11.5/ zh-cn/ColumnsConfig/CommonProperties/ItemsProcFunc.html

附加信息
普通記錄新記錄排在最前面的原因不是因為默認值是0。如果頁面上已經存在已經排好序的記錄,沒有position的新記錄排在最前面但已經分配了排序值。 一開始,記錄 1 和 2 的排序為 0,然后我手動排序了兩次,以獲得分配的排序值並再次進行相同的排序(它們在開始時已按 uid、標題或時間戳排序)然后我創建了記錄3 分配了排序值 128:

在此處輸入圖像描述

mm-tables 中的行為不同:

我可以重現所描述的行為,我有一個團隊記錄 (uid=3) 和 3 個排序的專業記錄 (uids: 1,2,3)。 然后我創建了一個新的專業記錄並分配了團隊記錄 3,結果排序值為 0:

在此處輸入圖像描述

數據庫級別的解決方案
為 MySql 寫了一個觸發器 function 我得到了一些有希望的結果,如果它被確認是正確的,我會很高興:

--
-- Triggers `tx_btp_team_expertise_mm`
--
DELIMITER $$
CREATE TRIGGER `btp_team_expertise_mm_insert_before` BEFORE INSERT ON `tx_btp_team_expertise_mm` FOR EACH ROW begin

declare uidLocal int(11) unsigned default 0;
declare uidForeign int(11) unsigned default 0;
declare sortingLocal int(11) unsigned default 0;
declare sortingForeign int(11) unsigned default 0;
declare maxSortingLocal int(11) unsigned default 0;
declare maxSortingForeign int(11) unsigned default 0;

-- TEAM (from expertise)
SELECT  new.uid_local into uidLocal;
SELECT  new.sorting into sortingLocal;
SELECT IFNULL(MAX(sorting),0) into  maxSortingLocal FROM tx_btp_team_expertise_mm WHERE uid_local = new.uid_local;

-- EXPERTISE (from team)
SELECT  new.uid_foreign into uidForeign;
SELECT  new.sorting_foreign into sortingForeign;
SELECT IFNULL(MAX(sorting_foreign),0) into maxSortingForeign FROM tx_btp_team_expertise_mm WHERE uid_foreign = new.uid_foreign;

IF maxSortingLocal > 0 AND sortingLocal = 0 THEN
    SET NEW.sorting = maxSortingLocal + 1;
ELSEIF maxSortingLocal = 0 AND sortingLocal = 0 THEN
    SET NEW.sorting = 1;
END IF;

IF maxSortingForeign > 0 AND sortingForeign = 0 THEN
    SET NEW.sorting_foreign = maxSortingForeign + 1;
ELSEIF maxSortingForeign = 0 AND sortingForeign = 0 THEN
    SET NEW.sorting_foreign = 1;
END IF;

end
$$
DELIMITER ;

要測試擴展/觸發器,您可以從我的存儲庫安裝演示和測試版本
充其量由作曲家: composer req wdb/trigger-sort-mm-table 請記住,除了像問題中描述的那樣在表上測試或演示觸發器之外,擴展沒有進一步的意圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM