Re: not understanding simple HABTM save/delete

2008-10-26 Thread RyOnLife


After hours and hours of frustration, I've figured it out. It wasn't fun, but
maybe not having my questions answered makes me a better programmer. For
future reference, it's actually pretty simple. I started with a new app with
my existing models and by baking new controllers/views I was able to figure
it out:

Since I'm applying a 'Label' to an 'Item', I just save() to my 'Item' model:

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

And pr($data) looks like this:

Array
(
[Item] => Array
(
[id] => 1
)
[Label] => Array
(
[Label] => Array
(
[0] => 4
[1] => 5
[2] => 7
[3] => 8
)
)
)

I also found that delete() isn't necessary. Before the new
associations/records are inserted in 'items_labels' for a given 'item_id'
(in the case, it's 1), all of the existing records for that 'item_id' are
deleted.
-- 
View this message in context: 
http://n2.nabble.com/not-understanding-simple-HABTM-save-delete-tp1377073p1380365.html
Sent from the CakePHP mailing list archive at Nabble.com.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



not understanding simple HABTM save/delete

2008-10-25 Thread RyOnLife


After spinning my wheels for a few hours on this one, any help would be very
appreciated...

I have an 'Item' model:

class Item extends AppModel {  
  var $name = 'Item';  
  var $hasAndBelongsToMany = 'Label';
}

A 'Label' model:

class Label extends AppModel {  
  var $name = 'Label';  
  var $hasAndBelongsToMany = 'Item';
}

And an 'items_labels' join table:

CREATE TABLE `items_labels` (
  `item_id` int(11) NOT NULL,
  `label_id` int(11) NOT NULL,
  `created` datetime default NULL
);

I cannot figure out how to save a simple record (association) to the join
table. I just don't understand what I need to put in my
'labels_controller.php' to get this result, for example:

INSERT INTO `database_name`.`items_labels` (`item_id`, `label_id`,
`created`) VALUES ('1', '1', NOW());

After many failed attempts, I'm back to an empty action:

function update($item_id, $label_id) {

}

In addition to save(), help with delete() would also be appreciated.

And not sure if it has any bearing on the solution, but for this action, I
am not concerned with any of the records in the 'items' or 'labels' tables.

Many thanks!

-Ryan
-- 
View this message in context: 
http://n2.nabble.com/not-understanding-simple-HABTM-save-delete-tp1377073p1377073.html
Sent from the CakePHP mailing list archive at Nabble.com.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---