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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

In a DataObject how to convert String Input Textfield to has_many DataObject


Go to End


7 Posts   2873 Views

Avatar
Allisone

Community Member, 27 Posts

17 March 2010 at 2:16am

Edited: 17/03/2010 2:23am

I have a wallpaper DataObject.
I'm managing my wallpaper in a DataObjectManager that is integrated in a category page.
A wallpaper has fields like title, image, rating and tags. The tags should be entered as String but stored in a seperate table....
So "blue, sky, nature" in a Textfield should be written into a table with a record for each tag like this
"blue"
"sky"
"nature"

First I had the tags as a simple string (sperated by ","). But as you start filtering wallpapers by tags and try to find other wallpapers with similar tags (like: show wallpapers that have the most amount of common tags) you want to use a second table and use sql's join queries.

So now I have a getCMSFields_forPopup() for my Picture Dataobject, that is being called, when adding/editing a picture to/of a gallery.
In this popup I have a nested DataObjectManager for the tags and it's not comfortable. Each time I add or edit a tag, the popup content is being replaced by the content of the add/edit dialog of the tags and when I'm finished adding/editing I can't get back to the original popup view. I can only press the general popup close and so the whole popup dissappears, although I didn't finish filling out the other fields (like choosing a title and other stuff) of the original popup view.

So either... is there a way to just close the inner DataObjectManager view instead of the whole popup or...
can I somehow have TextField for my tags where I can write something like "blue, sky, nature" but have a function that writes and reads those tags to/from other DataObjects that are bind by a has_many relationship ?

Here is a code excerpt:

<?php
class Wallpaper extends DataObject{
	static $db = array('Name'=>'Text');
	
	static $has_one = array('KategoriePage'=>'KategoriePage','WallpaperBild'=>'CustomImage');
	
	static $has_many = array('PictureTags'=>'PictureTag');

	public function getCMSFields_forPopup(){
			return new FieldSet(
				new ImageField('WallpaperBild','Das eigentliche Bild'),
				new TextField('Name','Name des Bildes'),
				new DataObjectManager($this,'PictureTags','PictureTag',PictureTag::$tabellen_namen,'getCMSFields_forPopup')
			);
	}
}
?>

<?php
class PictureTag extends DataObject{
	static $db = array('Name'=>'Text');

	static $has_one = array("Wallpaper"=>"Wallpaper");

	static $tabellen_namen = array('Name'=>'Tag');

	public function getCMSFields_forPopup(){
		return new FieldSet(
		new TextField('Name','Tag')
		);
	}
}
?>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

17 March 2010 at 2:45am

I would just do it in your onAfterWrite()..

public function onAfterWrite() {
parent::onAfterWrite();
if($this->Tags) {
foreach(explode(" ", $this->Tags) as $tag) {
$o = new Tag(array('SomeField' => $tag));
$o->write();
}
}
}

Avatar
Allisone

Community Member, 27 Posts

17 March 2010 at 3:15am

I haven't tried this, cause I don't understand how those onBeforeWrite onAfterWrite work, and because writing is only one thing, reading the other. Or should I just make a fake default value of the Textfield that is being newly generated (from the tags objects) each time getCMSFields_forPopup is loaded ?
But I will try to give it a try (right now busy with big sql query :D)

Avatar
UncleCheese

Forum Moderator, 4102 Posts

17 March 2010 at 3:44am

Well, a Tag object would have a foreign key that associates it back to the holder dataobject. So you'd probably want to add

$o->YourHolderPageID = $this->ID;

to the function, as well.

Avatar
Allisone

Community Member, 27 Posts

17 March 2010 at 3:53am

how do I prevent the textfield (with the string) from beeing written to the database as well

Avatar
Allisone

Community Member, 27 Posts

17 March 2010 at 4:18am

I will try to simply not to mention it in the $db array, but I am stuck now with the filling of the Textfield

In the popup method of the Wallpaper DataObject

	public function getCMSFields_forPopup(){
		$tagsObj = DataObject::get("PictureTag","WallpaperID = ".$this->ID);
		foreach($tagsObj as $tag){
			if($tag->Name != ""){
				$tags .= $tag->Name;
			}
		}
		
		return new FieldSet(
		new TextField('Name','Name des Bildes'),
		new TextField("all_tags","Tags",$tags),

But ID is not known, well it's known but it's always 0

Avatar
Allisone

Community Member, 27 Posts

17 March 2010 at 5:14am

Maybe I'm approaching this completly wrong ?