0

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.

Arno Chauveau
  • 990
  • 9
  • 14
azerto00
  • 1,001
  • 6
  • 18

2 Answers2

0

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.

Dakuipje
  • 91
  • 5
  • Here the thing, I have to create a Customer and an Address at the same time. So I don't have the id of the customer. But in the 2nd part of my question ("The only thing that is working [...] ") I tried in that way. – azerto00 Dec 08 '12 at 14:11
  • Well you could first save the Customer than use: `$customerID = $this->Customer->getInsertID();` to get the ID, and than save the address like I said. But there should be an easier way I guess, did you specify the relationship in your `Customer` model aswell ? – Dakuipje Dec 08 '12 at 14:26
  • Yes, that what I did, but it doesn't work, and the relationship are inside Customer model (written in question) – azerto00 Dec 08 '12 at 14:35
  • The relationships should be specified in both models. In your question you said it was in the address model btw. – Dakuipje Dec 08 '12 at 14:38
  • No, The relation is on Customer Model because Customer HABTM Address. Do I have to add a relation in Address model ? – azerto00 Dec 08 '12 at 14:45
  • Yes you should specify it in both: Address model: `public $hasAndBelongsToMany = array( 'Customer' => array( 'className' => 'Customer', 'joinTable' => 'address_customers', 'foreignKey' => 'customer_id', 'associationForeignKey' => 'adress_id' ) );` Customer ModeL: `public $hasAndBelongsToMany = array( 'Address' => array( 'className' => 'Address', 'joinTable' => 'address_customers', 'foreignKey' => 'address_id', 'associationForeignKey' => 'customer_id' ) );` – Dakuipje Dec 08 '12 at 14:51
  • [Sent before last comment] I created a new action in Customer model which add an Address. I follow everything from http://book.cakephp.org/2.0/en/models/saving-your-data.html but I still not have the relation table filled ! Only Address table. It seems like cakePhp doesn't automatically save the HABTM data to the database – azerto00 Dec 08 '12 at 14:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20803/discussion-between-azerto00-and-dakuipje) – azerto00 Dec 08 '12 at 14:55
0

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'  
));
azerto00
  • 1,001
  • 6
  • 18
Dakuipje
  • 91
  • 5