cakePHP HABTM don't save

2019-08-31 08:40发布

问题:

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.