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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

batch add to relationtable without $form->saveInto...


Go to End


6 Posts   2271 Views

Avatar
dio5

Community Member, 501 Posts

27 November 2007 at 10:02am

How can I fill the Tip_Tags table when I'm not using $form->saveInto(); ?

If you take a look at my code http://pastie.caboo.se/122204 around line 29 I'm doing it all wrong...

I'm wanting to 'batch' fill the tags table and fill the relationship table at once.
The tags part is easy...

Avatar
dio5

Community Member, 501 Posts

27 November 2007 at 10:35am

Solved.

$Tip->Tags()->add($savedTag);
$Tip->write();

Easy, but you gotta know it. :-)

Avatar
Sean

Forum Moderator, 922 Posts

27 November 2007 at 11:57am

The output of Tags() is a ComponentSet I assume?

Sean

Avatar
dio5

Community Member, 501 Posts

27 November 2007 at 12:14pm

I guess so.. they're in a many_many relation, but somehow I missed out that you could do
$Tips->Tags()->add();

Avatar
Sean

Forum Moderator, 922 Posts

27 November 2007 at 12:18pm

Yeah, that's definitely a ComponentSet then.

If you have relationships like has_many many_many etc, then the results return a ComponentSet which is a sub-class of DataObjectSet (exactly the same as a DataObject::get() call, but with extra relationship features).

The great thing is you can also use remove(), getIdList(), addMany(), removeMany() or any other useful methods in ComponentSet for managing the relationship.

Cheers,
Sean

Avatar
dio5

Community Member, 501 Posts

27 November 2007 at 11:38pm

Thanks, that's great!