CakePHP HABTM Question

Perhaps this is a question for beginners, as I am trying to understand that all these "PHP frameworks" are in my free time.

To get started, I want to add some tags to several photos. I have a tag model and a moto model (photos). Snip mot-model:

var $hasAndBelongsToMany = array(
                                    'Tag' =>
                                    array(
                                        'className'              => 'Tag',
                                        'joinTable'              => 'mots_tags',
                                        'foreignKey'             => 'mot_id',
                                        'associationForeignKey'  => 'tag_id',
                                        'unique'                 => false
                                    )
                                );

In my tags, the controller in add () has me:

$this->Tag->save($this->data);

When print_r'ing $ this-> data I see:

Array

([Mot] => Array ([id] => 2)

[Tag] => Array
    (
        [title] => 21e21e
    )

)

The tag is inserted into the tag table, but nothing is inserted into the mottags (underscore between the mot and the tag, but it is italic when I write it here, and not become the underscore). My schema mys_tags db: (sqlite)

create table mots_tags (id INTEGER PRIMARY KEY, mot_id INTEGER, tag_id INTEGER)

, Cake , ? SQL-. , ?

0
2

Try

$this->Tag->saveAll($this->data);

Edit:

, saveAll(). , HABTM , . , :

array(
   'Tag' => array('title' => ...),         // primary model
   'Mot' => array(                         // connected HABTM model
      'Mot' => array($id, $id, $id, ...)
   )
);
+2

.

Mot , Mots, mots, $hasAndBelongsToMany. , mot-, (-), bu Mot (s):

:

var $hasAndBelongsToMany = array(
    'Mot' =>
    array(
        'className'              => 'Mot',
        'joinTable'              => 'mots_tags',
        'foreignKey'             => 'tag_id',
        'associationForeignKey'  => 'mot_id',
        'unique'                 => false
    )
);
0

Source: https://habr.com/ru/post/1713919/


All Articles