0

data CakePHP:

Array (
    [Table_1] => Array (
        [name] => Test
    )
    [Table_2] => Array (
        [0] => Array (
            [name] => Test
        )
       [1] => Array (
            [name] => Test
        )
    )
)

SQL:

CREATE TABLE Table_1 (
    id INT AUTO_INCREMENT,
    name VARCHAR ( 10 ),
    PRIMARY KEY ( id )
)
CREATE TABLE Table_2 (
    id INT AUTO_INCREMENT,
    name VARCHAR ( 10 ),
    PRIMARY KEY ( id )
)
CREATE TABLE Table_1_2 (
    id INT AUTO_INCREMENT,
    table1_id INT,
    table2_id INT,
    PRIMARY KEY ( id ),
    FOREIGN KEY ( table1_id ) REFERENCES Table_1( id ),
    FOREIGN KEY ( table2_id ) REFERENCES Table_2( id )
)

The form shall register the three tables there described how to do this in CakePHP and still associate them properly? I tried different ways and could not.

paulorwd
  • 13
  • 3
  • 1
    Can you share the code of one or more of the ways you tried? – lenz Jun 12 '15 at 21:41
  • used Table1->saveAll, did not work, do not know if the data is formatted correctly or what. used save on a model at a time but once you get in HABTM table can not continue. Out of the various parameters used in both and modifications of the data structure. – paulorwd Jun 12 '15 at 21:47
  • Hi and welcome to Stack Overflow. Here we expect you to show us what you've tried. If you've tried several ways, show us the best you've got so far (even if it isn't working). Don't put the code in the comments (code formatting is awful), edit your question and add it there. show us actual code, not just a verbal description of it - we can't debug a verbal description. :) – Taryn East Jun 15 '15 at 06:46

1 Answers1

0

Here's a good article on CakePHP's HABTM: http://patisserie.keensoftware.com/en/pages/how-to-save-habtm-data-in-cakephp

Most important:

Associating existing records:

array(
    'Foo' => array(
        'id' => '...',
        ...
    ),
    'Bar' => array(
        'Bar' => array(
            [0] => 'id1', // id of an existing Bar
            [1] => 'id2', // id of another existing Bar
            ...
        )
    )
)

Associating new records:

array(
    [0] => array(
        [Foo] => array(
            [id] => ...
        ),
        [Bar] => array(
            [name] => ...
        )
    ),
    [1] => array(
        [Foo] => array(
            [id] => ...
        ),
        [Bar] => array(
            [name] => ...
            )
        )
)

There's a little more in the article on combined associations workaround.

Skatch
  • 2,112
  • 2
  • 14
  • 32
  • But are not existing records, are new records, ie, creates one for Table1 and Table2 and another for establishing an association. – paulorwd Jun 14 '15 at 14:55
  • Well yeah, that's the second example... It creates new records and connects them. What is the problem exactly? – Skatch Jun 14 '15 at 17:19