You have to populate the related models Id in your array, as it is
explained in the CakePHP book at:
http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM

So for each Tag record, add the related Artist record with just the
primary key of the Artist record.
Enjoy,
   John

On Aug 5, 2:29 pm, mr_robot <black.miy...@gmail.com> wrote:
> hi there
> been pulling my hair out on this one for 2 days.
> i have two simple models.
> artist and tags.
>
> and i have a artists_tags database table with artist_id and tag_id.
>
> artist model....
> class Artist extends AppModel {
>
>         var $name = 'Artist';
>         var $hasAndBelongsToMany = array(
>         //      'Tag',
>                 'Tag' => array(
>                         'className' => 'Tag',
>                         'joinTable' => 'artists_tags',
>                         'foreignKey' => 'artist_id',
>                         'associationForeignKey' => 'tag_id',
>                         'unique' => true
> )
>
> }
>
> tags model:
> class Tag extends AppModel {
>
> var $name = 'Tag';
> var $hasAndBelongsToMany = 'Artist';
>
> }
>
>  i can read back values fine. which tells me my associations are
> working correctly.
>
> if i do a print_r($artist) i get:
>
> Array
> (
>     [0] => Array
>         (
>             [id] => 793
>             [name] => ebm
>             [link] =>http://www.bob.com/tag/ebm
>             [source] => 91
>             [rank] =>
>             [ArtistsTag] => Array
>                 (
>                     [id] => 20
>                     [artist_id] => 134426
>                     [tag_id] => 793
>                 )
>
>         )
>
> )
>
> the problem lies in writing the data. i am not using a form, but
> wanting to add data programatically. i fetch a whole lot of data from
> an API then process and insert. this method is working fine for all my
> HAS_MANY associations.
>
> so just trying to manually insert data here fails. it inserts the data
> into the tag model, but never populated the artist_tags table.
>
> if i try something like this:
> $artist = $this->Artist->findById(123456);
> $artist['Tag'][1]['name'] = "rock5";
> $artist['Tag'][1]['link'] = "http://www.last.fm/tag/ebm";;
> $artist['Tag'][1]['artist_id'] = 184287;
> $artist['Tag'][1]['source'] = "Last.fm";
> $artist['Tag'][1]['rank'] = 91;
>
> $this->Artist->Tag->save($artist['Tag'], false);
> or any other variations of save. no luck.
> // none of these work:
> //$this->Artist->Tag->saveAll($artist['Tag']);
> // $this->Tag->save($artist['Tag']);
>
> the sql trace is as follows:
> 45      INSERT INTO `tags` (`name`, `link`, `source`, `rank`) VALUES
> ('rock5', 'http://www.last.fm/tag/ebm', 'Last.fm', 91)
> 46      SELECT LAST_INSERT_ID() AS insertID
> 47      COMMIT
> 48      SELECT COUNT(*) AS `count` FROM `tags` AS `Tag` WHERE `Tag`.`id` =
> 842
> 49      START TRANSACTION
>
> any ideas, help or pointers would be much appreciated.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to