CakePHP 4.x:如何创建一组表单控件,列出 HABTM直通关联中的所有值?

问题描述

我正在尝试基于联系人创建单个编辑表单,即使联系人还没有评级,它也允许我更新/添加所有技能的技能级别。

类似于:

Edit Contact

Name   [TextBox: John Doe]

Skills
C#              [TextBox: empty]
C/C++           [TextBox: empty]
Data Analysis   [TextBox: 5]
Graphic Design  [TextBox: empty]
Javascript      [TextBox: 8]
Photography     [TextBox: empty]
HTML            [TextBox: empty]
Json            [TextBox: empty]
Jquery          [TextBox: empty]

我的表是这样配置的:

联系方式

TABLE `contacts` (
  `id` bigint(20) NOT NULL,`name` varchar(100) NOT NULL
)

(1,'John Doe')

$this->belongsToMany('Skills',[
    'foreignKey' => 'contact_id','targetForeignKey' => 'skill_id','joinTable' => 'contacts_skills',(Tried with and without)
    'through' => 'ContactsSkills',(Tried with and without)
]);

技能

TABLE `skills` (
  `id` bigint(20) NOT NULL,`name` text NOT NULL
)

(1,'C#'),(2,'C/C++'),(3,'Data Analysis'),(4,'Graphic Design'),(5,'Javascript'),(6,'Photography'),(7,'HTML'),(8,'Json'),(9,'Jquery')

$this->belongsToMany('Contacts',[
    'foreignKey' => 'skill_id','targetForeignKey' => 'contact_id',(Tried with and without)
]);

联系人技能

TABLE `contacts_skills` (
  `id` bigint(20) NOT NULL,`contact_id` bigint(20) NOT NULL,`skill_id` bigint(20) NOT NULL,`level` tinyint(1) DEFAULT NULL
)

(1,1,3,5),5,8)

$this->belongsTo('Contacts','joinType' => 'INNER',]);
$this->belongsTo('Skills',]);

我已经挖掘了文档: https://book.cakephp.org/4/en/orm/saving-data.html https://book.cakephp.org/4/en/views/helpers/form.html(我在那里了解了 _joinData)注意:多选元素没有 为 _joinData 工作。

And many more.  : )

我尝试了几乎所有关联组合来将三个表连接在一起,但无济于事。

我知道我可以使用_joinData来获取已经有等级的技能:

echo $this->Form->control('skills.0._joinData.level');
echo $this->Form->control('skills.1._joinData.level',['label' => ?????]);
            

虽然我在将标签设置为技能名称时遇到问题。

我的假设是我必须迭代技能表并以这种方式构建控件,但我不知道如何从迭代中引用连接表。

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)