php - CakePHP 保存 HABTM 数据

标签 php mysql forms cakephp

我有 2 个模型,ClientSecurity。它们与 HATBTM 关系相关联。 我制作了一个名为 clients_securities 的连接表。所以一个Client可以有很多证券,一个Security可以属于很多Client

enter image description here

这是我的关系定义:

 //Client Model:
public $hasAndBelongsToMany = array(
    'Security' =>
        array(
            'className' => 'Security',
            'joinTable' => 'clients_securities',
            'foreignKey' => 'client_id',
            'associationForeignKey' => 'security_id',
            'unique' => true,
        )
);

 //Security Model:
public $hasAndBelongsToMany = array(
    'Client' =>
        array(
            'className' => 'Client',
            'joinTable' => 'clients_securities',
            'foreignKey' => 'security_id',
            'associationForeignKey' => 'client_id',
            'unique' => true,
        )
);

然后我在客户端 Controller 中进行了编辑操作并执行了此操作:

public function edit($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid client'));
        }

        $client = $this->Client->findById($id);
        if (!$client) {
            throw new NotFoundException(__('Invalid client'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->Client->id = $id;
            if ($this->Client->saveAll($this->request->data)) {
                $this->Session->setFlash(__('Client has been updated.'));
                return $this->redirect(array('action' => 'edit', $id));
            }
            $this->Session->setFlash(__('Unable to update client.'));
        }

        if (!$this->request->data) {
            $this->request->data = $client;
            $this->set('securities', $this->Client->Security->find('list'));
        }
    }

在我的编辑 View 中,我使用 HtmlHelper 构建表单,添加客户端表字段并生成多选:

echo $this->Form->create('Client'); //start the form for the client model
echo $this->Form->input('Security'); //generates a multi-select popuplated with securities

此外,我还以相同的形式为 Client 提供了正常的直接表单输入。例如

echo $this->Form->input('name'); //Input for the client name
echo $this->Form->input('currency'); //Input for the client currency
...

在呈现表单时,所有这些输入都会生成并填充正确的值,但只保存直接客户端数据,而不是来自多选的 HABTM 数据。

当我提交表单时,clients_securities 表没有填充连接 ID。

我需要做什么才能正确保存它,然后在我重新加载“编辑” View 时预先选择保存的证券。


编辑:为了澄清一些事情,这里是 $this->request->datapr()。 (值(“ALLCMCT LX Equity”)是 securities 表的正确外键):

Array
(
    [Client] => Array
        (
            [id] => 1212
            [name] => Example Client
            [currency] => GBP
            [partner] => 
            [risk_category_id] => 4
            [client_code_id] => 1
            [min_cash_balance] => 0
            [active] => 1
            [model] => 0
            [institutional] => 0
            [non_uk_situs] => 0
            [reporting_status] => 0 
        )

    [Security] => Array
        (
            [Security] => Array
                (
                    [0] => .1muslib3 index
                    [1] => .eurib3 index
                    [2] => .ukcpi2 index
                )

        )

)

所以基本上,假设我的客户 ID 是 12345,Cake 应该在 clients_securities 表中插入 2 条记录,如下所示:

id | client_id | security_id
----------------------------
1  | 12345     | ALLCMCT LX Equity
2  | 12345     | APMKNTD LX Equity

如果我手动将一些连接记录添加到 clients_securities 中,当我去编辑客户端时,多选中的证券会正确预选,表明正在从连接中读取数据 table 。当我保存表单时,它实际上会删除连接记录,但不会保存新记录。

附加说明:我的安全 ID 存储为 CHAR(36)(如果有任何影响)。这不是一个自动递增字段,它包含证券的代码并且每个都是唯一的。

最佳答案

问题出在 security_idSecurity.id 的格式上:不支持 char。

我找到了 ticket (但我没有在文档中找到任何引用)

您必须创建另一个具有数字 id 的列以在 HABTM 关联中使用

关于php - CakePHP 保存 HABTM 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893392/

相关文章:

php - 使用 wordpress 将图像和标题输出到 Bootstrap 轮播中

php - 在下拉框中进行选择后显示文本值

MySQL - CET 和 CEST 之间的区别

javascript - 多个相关选择框不起作用

php - 尝试获取正在查看的个人资料的用户 ID

php strtotime 反转

mysql查找丢失的项目

Mysql集群问题

forms - Drupal:以多步形式设置 id 属性

javascript - JQuery:验证表单中的所有输入