【问题标题】:cakePHP HABTM don't savecakePHP HABTM 不保存
【发布时间】:2012-07-26 08:24:13
【问题描述】:

我已经查看了我能找到的关于这个主题的所有线程,但没有任何解决我的问题。

我有一个 'customers' 表和一个 'employees' 表,它们应该通过 'customers_employees' 表连接。

在我的表单中我可以选择员工,但是当我想保存数据时,他只保存“创建”和“修改”条目而不是键。

员工有一个字符串 id,它是 CHAR(36)。

这是我在“员工”模型中的条目:

public $hasAndBelongsToMany = array(
    'Customer' => array(
        'className' => 'Customer',
        'joinTable' => 'customers_employees',
        'foreignKey' => 'employee_id',
        'associationForeignKey' => 'customer_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

这是我在“客户”模型中的条目:

public $hasAndBelongsToMany = array(
    'Employee' => array(
        'className' => 'Employee',
        'joinTable' => 'customers_employees',
        'foreignKey' => 'customer_id',
        'associationForeignKey' => 'employee_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

请求数组如下所示:

Array
(
    [Customer] => Array
        (
            [id] => 10000
            [name] => XXXXX
            [name2] => XXXXX
            [address] => XXXXX
            [address2] => 
            [country_code] => 
            [plz] => 12345
            [place] => XXXXX
            [tel] => XXXXX
            [fax] => XXXX
            [mail] => XXXXX
            [customer_price_group] => XX
            [allow_discount_line] => XX
            [payment_code] => 0
            [terms_of_delivery_id] => XXX
            [closed] =>  
            [uSt_IdNr] => DEXXXXXXXXX
            [information] => 
            [conditions] => XX
        )

    [Employee] => Array
    (
        [Employee] => Array
            (
                [0] => EMPXXX
            )

    )

)

这里是对 customers_employees 的 INSERT 查询:

INSERT INTO `appsbf_db6`.`customers_employees` (`modified`, `created`) VALUES    ('2012-07-26 06:40:22', '2012-07-26 06:40:22')

我的错误在哪里?

谢谢帮助!

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-model


    【解决方案1】:

    请求数组中的员工数据应如下所示:

    [Employee] => Array
    (
        [0] => Array
            (
                [Employee] => Array
                            (
                              [first_name] = ADSF
                              .........
                              ......... 
                            )
            )
    
        [1] => Array
            (
                [Employee] => Array
                            (
                              [first_name] = wersdf
                              .........
                              ......... 
                            )
            )
    
    )
    

    在您看来,请使用以下表单元素:

     $this->Form->input('Employee.0.FirstName', array(...........
    

    希望它对你有用。

    【讨论】:

    • 您好,感谢您的回答。但我需要的是一个可以选择更多客户的列表。 Cake 生成了以下代码: echo $this->Form->input('Employee', array('label' => 'Mitarbeiter'));
    • 你应该尝试使用:echo $this->Form->input('Employee][', array('label' => 'Mitarbeiter'));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2013-09-24
    • 2012-08-08
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多