Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba

HOWTO: HasMany and ManyMany new items default to checked


Go to End
Reply


3 Posts   2100 Views

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 August 2010 at 12:28pm

I posted this solution a while ago. You shouldn't need to update the javascript or anything.

This is what I do:

	public function onAfterWrite()
	{
		parent::onAfterWrite();
		if(isset($_REQUEST['ctf'])) {
			// new record?
			if(!isset($_REQUEST['ctf']['childID']))
				DB::query("INSERT INTO `[YOUR_JOIN_TABLE]` SET [PARENT_TABLE_ID] = ". $_REQUEST['ctf']['sourceID'] . ", [CHILD_TABLE_ID] = $this->ID");
	

		}
	}

Avatar
UncleCheese

Forum Moderator, 4096 Posts

18 August 2010 at 1:13am

Oh, right. Yeah, I forgot about that hack. Cool, thanks for the heads up.

Avatar
Garrett

Community Member, 245 Posts

8 March 2012 at 11:20am

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