Re: HABTM can't save manually

2011-08-09 Thread mr_robot
hi john
thanks for all the help (and the kind encouragement).

i kinda of worked out a solution, posting here for others:

$tag_count = count($artist['Tag']);
$artist = $this->Tags->checkforUpdates($artist);

if (count($artist['Tag'])>$tag_count){

// first save all the tags
foreach($artist['Tag'] as $tag){
$t['Tag'] = $tag;
$t['Artist']['Artist'][0] = $artist['Artist']['id'];
$this->Tag->create();
$this->Tag->save($t);
}

// now del. all the artists assoc. records
$this->ArtistsTag->deleteAll(array('ArtistsTag.artist_id' => $id));

// now save the associations
$i = 0;

foreach($artist['Tag'] as $tag)
 {

$t = $this->Tag->findByName($tag['name']);

// then associate the correct IDs
$this->data[$i]['ArtistsTag']['artist_id']= $id;
$this->data[$i]['ArtistsTag']['tag_id'] = $t['Tag']['id'];

$i++;
 }
// save the assoc.
$saved = $this->ArtistsTag->saveAll($this->data);
}


this seems a very messy and long winded way of doing things. and kinda
of feels like it defeats the object of active record associations. but
it works!

-- 
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


Re: HABTM can't save manually

2011-08-09 Thread John Andersen
When using that behavior, you must be sure when adding a new Tag to an
Artist, that there already exists at least one other Tag for the
Artist.
The behavior should be modified to just add the new one. That is what
I understand from looking at the source code.

You could also go for the solution, which is mentioned at the bottom
of the page about saving HABTM data in the CakePHP book at:
http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM

Keep up the good work, enjoy,
   John

On Aug 9, 8:27 pm, mr_robot  wrote:
> hi john. yea it's 1.
>
> i have got it working another way with this kind of array:
> $tags = array (
>     0 => array (
>         'Tag' => array (
>              'id' => 123458,
>                         'name' => 'test_tag3',
>             'link' => 'xxx',
>             'source' => 'yyy',
>             'rank' => 2
>         ),
>         'Artist' => array (
>             'Artist' => array (
>                 0 => 7 // this being the artist_id
>             )
>         )
>     ),
>     1 => array (
>         'Tag' => array (
>             'id' => 123456,
>             'name' => 'existing_tag',
>             'link' => 'xxx',
>             'source' => 'yyy',
>             'rank' => 3
>         ),
>         'Artist' => array (
>             'Artist' => array (
>                 0 => 7 // this being the artist_id
>             )
>         )
>     ),
> );
>
> but now it's deleting the already saved associations.
> which leads me to this habtmAdd 
> behavior.http://bakery.cakephp.org/articles/bparise/2007/05/09/add-delete-habt...
> seems like i'm on the right track. but
>
> $this->Artist->habtmAdd('Tag', 1, 123456); //artist_id=1, tag id =
> 123456
>
> does nothing.

-- 
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


Re: HABTM can't save manually

2011-08-09 Thread mr_robot
hi john. yea it's 1.

i have got it working another way with this kind of array:
$tags = array (
0 => array (
'Tag' => array (
 'id' => 123458,
'name' => 'test_tag3',
'link' => 'xxx',
'source' => 'yyy',
'rank' => 2
),
'Artist' => array (
'Artist' => array (
0 => 7 // this being the artist_id
)
)
),
1 => array (
'Tag' => array (
'id' => 123456,
'name' => 'existing_tag',
'link' => 'xxx',
'source' => 'yyy',
'rank' => 3
),
'Artist' => array (
'Artist' => array (
0 => 7 // this being the artist_id
)
)
),
);

but now it's deleting the already saved associations.
which leads me to this habtmAdd behavior.
http://bakery.cakephp.org/articles/bparise/2007/05/09/add-delete-habtm-behavior
seems like i'm on the right track. but

$this->Artist->habtmAdd('Tag', 1, 123456); //artist_id=1, tag id =
123456

does nothing.

-- 
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


Re: HABTM can't save manually

2011-08-09 Thread John Andersen
Ok, you have tried that way, but maybe your problem wasn't explained
correctly!
Is your problem one of the following:
1) You want to add tags to an existing artist. The existing artist may
already have some tags and they must be preserved.
2) You want to add a new set of tags to an existing artist, thus
replacing all previous tags added to the artist (this is how HABTM
normally works).
3) You want to do something different from 1 and 2 above.

In 1) you probably have to consider using a HasMany relationship
between Artist and ArtistTag instead of the HABTM between Artist and
Tag.
In 2) you can save more than one tag with each Artist. Just add each
tag to your array and save the Artist.
In 3) more information is needed :)

Enjoy,
   John

On Aug 9, 4:13 pm, mr_robot  wrote:
> so found a solution. sort of. after building some forms and looking at
> the output, i see the format as follows:
>
>     [Tag] => Array
>         (
>             [name] => final2
>             [rank] =>
>         )
>
>     [Artist] => Array
>         (
>             [Artist] => Array
>                 (
>                     [0] => 1903895
>                     [1] => 1903896
>                 )
>
>         )
>
> so have been trying to reconstruct the array, which works:
>
> foreach($artist['Tag'] as $tag){
> $t['Tag'] = $tag;
> $t['Artist']['Artist'][0] = $artist['Artist']['id'];
> //print_r($t);
> //$this->Tag->saveAll($artist['Tag']);
> $this->Tag->create();
> //$this->Tag->save($t, false);
> $this->Tag->save($t);
>
> }
>
> however, it is not saving the tags in the artist_tags table if the
> tags already exist. ie. the validation is stopping the record from
> being saved.
>
> any ideas?
> this also seems to be a very long winded way of doing things. am i
> still missing something?

-- 
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


Re: HABTM can't save manually

2011-08-09 Thread mr_robot
so found a solution. sort of. after building some forms and looking at
the output, i see the format as follows:

[Tag] => Array
(
[name] => final2
[rank] =>
)

[Artist] => Array
(
[Artist] => Array
(
[0] => 1903895
[1] => 1903896
)

)

so have been trying to reconstruct the array, which works:

foreach($artist['Tag'] as $tag){
$t['Tag'] = $tag;
$t['Artist']['Artist'][0] = $artist['Artist']['id'];
//print_r($t);
//$this->Tag->saveAll($artist['Tag']);
$this->Tag->create();
//$this->Tag->save($t, false);
$this->Tag->save($t);
}


however, it is not saving the tags in the artist_tags table if the
tags already exist. ie. the validation is stopping the record from
being saved.

any ideas?
this also seems to be a very long winded way of doing things. am i
still missing something?

-- 
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


Re: HABTM can't save manually

2011-08-08 Thread mr_robot
. no luck adding the whole $artist array. it starts to do weird
stuff - adding values from the artist table into the artist_tags
table. very confusing.
will try build a form but this is all very strange - i assumed
relations would be straightforward...?

-- 
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


Re: HABTM can't save manually

2011-08-08 Thread John Andersen
Yes, I saw that, but the book says you have to do it differently in
order to do it automatically.

You have to refer to the Artist like this:
[code]
$artist['Artist']['id'] = 184287;
[/code]

Observe that you should not just call the save method with the
$artist['Tag'] data only, but the full $artist array.

Else just try to make a form with all the information, execute a save
and take a look at how the resulting array looks like.
Enjoy,
   John

On Aug 8, 9:33 am, mr_robot  wrote:
> hi john
>
> thanks for the answer. but i am doing this:
> $artist['Tag'][1]['artist_id'] = 184287;
>
> regards

-- 
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


Re: HABTM can't save manually

2011-08-08 Thread mr_robot
hi john

thanks for the answer. but i am doing this:
$artist['Tag'][1]['artist_id'] = 184287;

regards

-- 
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


Re: HABTM can't save manually

2011-08-07 Thread John Andersen
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  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