ItGo.me Focus on IT Recommend

Home > has and belongs to many - CakePHP HABTM question

has and belongs to many - CakePHP HABTM question

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

Might be a newbie question as I'm trying to see what all these "PHP frameworks" are at my free time.

For starters I want to add multiple tags to multiple photos. I have a tags model and mot model (the photos). Snip of mot model:

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

In my tags controller in add() I have:

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

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

Array

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

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

)

Tag get inserted into Tags table, but nothing gets inserted into mottags(theres underscore between mot and tag but it italics when i write it here instead of becoming an underscore) table. My mots_tags db schema: (sqlite)

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

Any clues why Cake writes only to Tags table and not to associacions table? I don't get any SQL errors. Is there a way to see if it tries to write to associations table at all?

cakephp has-and-belongs-to-many
|
  this question
asked Jul 23 '09 at 7:17 jpou 665 1 6 19

 | 

2 Answers
2

Try

Recommend:has and belongs to many - CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

$hasAndBelongsToMany variable set. When I managing User Locations, I want to delete the association between the User and Location, but not the Location. Clearly this Location could belong to other users. I would expect the following code t

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

Edit:

Well, you definitely need saveAll(). Additionally, the array of the connected HABTM model needs to be in a certain, slightly curious format. If I remember correctly, it should look like this:

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

|
  this answer
edited Jul 23 '09 at 7:43 answered Jul 23 '09 at 7:20 deceze ♦ 319k 49 414 557      Same - Nothing goes to mots_tags. No errors, Tag reaches tags table. –  jpou Jul 23 '09 at 7:29

 | 

Found a solution myself.

Because Mot's can have many tags, and Tags can have many Mots, and Tags adding is handled by tags controller instead of mots controller, tag model must also contains $hasAndBelongsToMany. Same as in mot model, just with Tag(s) replaced bu Mot(s):

This should have been in tag model also, not only in mot model:

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

|
  this answer
edited Jan 17 '16 at 6:35 Pang 5,598 14 48 80 answered Jul 23 '09 at 8:09 jpou 665 1 6 19

 | 

Recommend:has and belongs to many - CakePHP find HABTM

e Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this-

------splitte line----------------------------