CakePHP保存HABTM数据 [英] CakePHP saving HABTM data

查看:182
本文介绍了CakePHP保存HABTM数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型,客户安全。它们与HATBTM关系相关联。
我创建了一个名为 clients_securities 的连接表。因此,客户可以有很多证券,安全可以属于许多 c $ c> s。





这里是我的关系定义:

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

);

//安全模型:
public $ hasAndBelongsToMany = array(
'Client'=>
array(
'className'=> Client',
'joinTable'=>'clients_securities',
'foreignKey'=>'security_id',
'associationForeignKey'=>'client_id',
' unique'=> true,

);

然后我在客户端控制器中进行了编辑操作,并执行了以下操作:

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

$ 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(__已更新。
return $ this-> redirect(array('action'=>'edit',$ id));
}
$ this-> Session-> setFlash(__('无法更新客户端。
}

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



在我的编辑视图中,我使用HtmlHelper ,添加客户端表字段并生成多个选择:

  echo $ this-> Form-> create客户'); //启动客户端模型的表单
echo $ this-> Form-> input('Security'); //生成一个多选择弹出的证券

此外,我也有正常的直接表单输入客户端。例如

  echo $ this-> Form-> input('name'); //输入客户端名称
echo $ this-> Form-> input('currency'); //客户货币的输入
...



当我提交表单时,会显示正确的值,但只保存直接客户端数据,而不保存HABTM数据。 code> clients_securities 表未填入连接ID。



我需要做什么才能正确保存,






编辑:为了澄清一下,这里是 $ this-> request-> data pr()
(值(ALLCMCT LX Equity)是证券表的正确外键:

  Array 

[Client] => Array

[id] => 1212
[ name] =>示例客户
[currency] => GBP
[合作伙伴] =>
[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 |如果我手动添加一些连接记录到 clients_securities c $ c>,当我去编辑一个客户时,在多重选择中正确预选的证券表明正在从连接表中读取数据。



其他注意事项:我的安全性ID会储存为CHAR(36)如果有任何效果。这不是一个自动增量字段,它包含安全性的代码,每个都是唯一的。

解决方案

问题出在不支持 security_id Security.id :char的格式。



我发现这个 ticket (但我在文档中找不到任何引用)



您必须创建另一个数字 id 用于HABTM关联


I have 2 models, Client and Security. They are associated with a HATBTM relationship. I have made a join table called clients_securities. So a Client can have many securities and a Security can belong to many Clients.

Here are my relationship definitions:

 //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,
        )
);

I then made an edit action in my clients controller and did this:

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'));
        }
    }

In my edit view I build the form using the HtmlHelper, adding the client table fields and generating the multiple select with:

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

In addition I also have normal direct form inputs for the Client in the same form. e.g.

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

All these inputs get generated and populated with the correct values when the form is rendered but only the direct Client data is saved, not the HABTM data from the multi-select.

When I submit the form the clients_securities table is not populated with the join IDs.

What do I need to do to save it correctly and have then have the saved securities be pre-selected when I reload the "edit" view.


Edit: To clarify things, here is a pr() of $this->request->data. (The values ("ALLCMCT LX Equity") are the correct foreign keys for the securities table):

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
                )

        )

)

So essentially, say my client id was 12345, Cake should insert 2 records in the clients_securities table like so:

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

If I manually add some join records into clients_securities, when I go to edit a client the securities in the multi-select come up correctly pre-selected showing that the data is being read from the join table. When I save the form it actually removes the join records but does not save new ones.

Additional note: My security IDs are stored as CHAR(36) if that has any effect. This is not an auto-increment field, it contains the ticker of the security and each is unique.

解决方案

The problem is in the format of the security_id and Security.id: char is unsupported.

I found this ticket (but I don't find any reference in the documentation)

you must create another column with a numeric id to use in the HABTM association

这篇关于CakePHP保存HABTM数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆