Hey gents--
I'm trying to do the opposite -- I have a Category DataObject which has a $belongs_many_many relationship to a NewsPage, which has the corresponding $many_many there. As a result I have the relation table NewsPage_Categories.
What I'd like to do is to automatically associate a given Category with a newly created page so that there is always at least one Category selected. Seems like it would just be a simple INSERT INTO in an OnAfterWrite() function but alas, no such luck. It's not happening for me. here's my very simple code (in the NewsPage class):
function onAfterWrite() {
parent::onAfterWrite();
if(isset($_REQUEST['ctf'])) {
DB::query("INSERT INTO `NewsPage_Categories` SET `NewsPageID` = '".$_REQUEST['ctf']['sourceID']."', `CategoryID` = '1'");
}
}
What's wrong with this picture? I surmised from the code posted above that the $_REQUEST['ctf']['sourceID'] is the ID of the newly created Page. But I am not getting an error; it's just that the insert doesn't occur. onAfterWrite(), however, is firing.
Thanks in advance,
Garrett