PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] CakePHP and HABTM insert/save doesn't work

 January 28, 2022     cakephp, has-and-belongs-to-many, php     No comments   

Issue

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.


Solution

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


Answered By - Dakuipje
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing