简体   繁体   中英

CakePHP and HABTM insert/save doesn't work

I'm stuck in Cakephp insert, here is what I have

-Order HABTM Address

-Address model

Here is my association in Address model :

public $hasAndBelongsToMany = array(
    'Address' => array(
        'className' => 'Address',
        'joinTable' => 'address_customers',
        'foreignKey' => 'customer_id',
        'associationForeignKey' => 'address_id'  
    )
);

I want to save/insert a new customer with one new address So, I thought the array below would work :

array(
'Customer' => array(
    'nom' => 'Khiami',
    'prenom' => 'TEST',
    'tel' => '0945454545',
    'ptel' => '',
    'commentaire' => '',
    'email' => '',
    'anniversaire' => array(
        'day' => '08',
        'month' => '12',
        'year' => '2012'
    ),
    'createdFrom' => 's'
),
'Address' => array(
    (int) 0 => array(
        'adresse' => 'ADDR TEST',
        'cp_id' => '1',
        'etage' => '',
        'residence' => '',
        'batiment' => '',
        'appartement' => '',
        'type' => 'l',
        'code_sonette' => '',
        'code_portail' => '',
        'code_batiment' => '',
        'code_asc' => ''
    )
)
)

The only thing that is working is when I save the Customer first and then use the array below :

array(
(int) 0 => array(
    'Customer' => array(
        'customer_id' => '394'
    ),
    'Address' => array(
        'adresse' => 'ADDR TEST',
        'cp_id' => '1',
        'etage' => '',
        'residence' => '',
        'batiment' => '',
        'appartement' => '',
        'type' => 'l',
        'code_sonette' => '',
        'code_portail' => '',
        'code_batiment' => '',
        'code_asc' => ''
    )
)
)

But, I still don't have the association table filled ! It only adds an entry in the Address table.

Since you specify the jointable in your HABTM relationship I doubt its a naming convention problem.

Are you saving in the Address or the Customer model ? Because if you are calling it from the Address model I suppose there should already be a customer, in that case you only have to set:

$this->request->data['Customer']['Customer'] = $customer_id; 

And save the address data, it should create the HABTM association in the table.

Yes you should specify it in both:

Address model:

public $hasAndBelongsToMany = array(
   'Customer' => array(
       'className' => 'Customer',
       'joinTable' => 'address_customers',
       'foreignKey' => 'address_id',
       'associationForeignKey' => 'customer_id'  
));

Customer Model:

 public $hasAndBelongsToMany = array(
   'Address' => array(
      'className' => 'Address',
      'joinTable' => 'address_customers',
      'foreignKey' => 'customer_id',
      'associationForeignKey' => 'address_id'  
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM