在我的扩展中,我有两个模型:团队成员(父级)和专才(子级)。两个模型之间的双向关系存储在中间表中。这工作得很好,有一个问题。
当我在后端编辑TYPO3团队成员的记录并将专门知识记录分配给它,然后打开相应的专门知识记录时,新的辅助团队成员记录将显示在列表的顶部而不是末尾。问题也是反过来发生的。
这是因为在保存记录时,中间表中的sorting_foreign字段分别设置为默认值(0)。
我怎么才能解决这个问题?
模型团队成员的TCA
...
'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
...
'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)
);发布于 2022-05-06 01:37:52
On数据库级您可以做一些事情,我下面链接的解决方案是根据现有值增加值,因此不考虑包含的页面或记录,这将导致比仅与单个页面相关的值更高的排序值:https://stackoverflow.com/a/3292500/1019850
同一页面上的另一个解决方案是使用触发器,这可能是一种很好的方法(请阅读我的评论以调整过时的代码):https://stackoverflow.com/a/3324116/1019850
这些代码示例中的任何一个都可以作为调整命题以满足自身需求并对其进行优化的基础。
On TYPO3 您可以包含一个函数itemsProcFunc,它可以包含检索现有值和增加新值的逻辑,这与所需的情况非常相似:https://docs.typo3.org/m/typo3/reference-tca/11.5/en-us/ColumnsConfig/CommonProperties/ItemsProcFunc.html
附加信息
在顶部排序新记录的常见记录的原因不是默认值为0。如果页面上已经存在已排序的记录,则在顶部对没有位置的新记录进行排序,但已经分配了排序值。在开始时,记录1和2有排序0,然后我手动排序两次,以获得分配的排序值,并再次进行相同的排序(它们在开始时通过uid、title或时间戳进行排序),然后我创建了记录3,其排序值为128:

mm表中的行为是不同的:
我可以重现所描述的行为,我有一个团队记录(uid=3),它有3个排序的专业记录(uids: 1,2,3)。然后我创建了一个新的专门知识记录并分配了团队记录3,结果排序值为0:

数据库级解决方案
为MySql编写了一个触发器函数之后,我得到了一些很有希望的结果,如果它被证实是正确的,我会很高兴的:
--
-- 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。请记住,扩展除了在问题中描述的表上测试或演示触发器之外,没有任何进一步的意图。
https://stackoverflow.com/questions/71844437
复制相似问题