cakePHP HABTM don't save

帅比萌擦擦* 提交于 2019-12-24 22:50:28

问题


I have looked into all threads I could find about this Topic, but nothing solves my Problem.

I have a 'customers' table and an 'employees' table which should be connected via the 'customers_employees' table.

In my Form I can select the employees, but when I want to save the data he only save the 'created' and 'modified' entry and not the keys.

The employee has an string id which is CHAR(36).

This is my entry in the 'employee' model:

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

This is my entry in the 'customer' model:

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

The request array looks like this:

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
            )

    )

)

Here the INSERT query to customers_employees:

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

Where is my mistake?

Thx for help!


回答1:


Your Employee data in request array should looks like:

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

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

)

In your view use the form elements like:

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

Hope it will work for you.



来源:https://stackoverflow.com/questions/11665239/cakephp-habtm-dont-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!