Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » In a DataObject how to convert String Input Textfield to has_many DataObject

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1589 Views
  • Allisone
    Avatar
    Community Member
    27 Posts

    In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    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')
          );
       }
    }
    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    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();
    }
    }
    }

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    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 )

  • UncleCheese
    Avatar
    4085 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    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.

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

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

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    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

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: In a DataObject how to convert String Input Textfield to has_many DataObject Link to this post

    Maybe I'm approaching this completly wrong ?

    1589 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.