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, simon_w

Checkboxes in popup only stored on second time save


Go to End
Reply


9 Posts   2407 Views

Avatar
vancouverWill

Community Member, 121 Posts

10 December 2009 at 1:39pm

So I find that everytime I create a new dataobject the checkboxes (which are called by Dataobject::get from another dataobject if that helps) never save when I first create the new dataobject. I often forget and then fill in a bunch of checkboxes only to lose the info when I save. After I try it again it always works but somehow the creation of the new dataobject doesn't let me save the checkboxes. also I can then continually change and save which checkboxes are selected, it is just the first time when there is always a problem. wondering if anyone else had this problem.

Avatar
UncleCheese

4086 Posts

11 December 2009 at 3:48am

Can you post your page and dataobject code so I can test this?

Avatar
vancouverWill

Community Member, 121 Posts

11 December 2009 at 7:12am

<?

class ProjectPage extends Page
{

static $db = array(
      
   );

static $has_many = array (
      'Projects' => 'Project',
      //'MyClients' => 'Client',
      //'MyIndustries' => 'Industry',
      //'MyBusinessAreas' => 'BusinessArea'
      //, 'MyEnvironments' => 'Environment'
   );
   
   function getCMSFields()
   {
      
      $projectFields = parent::getCMSFields();

      $projectfield = new dataObjectManager(
                        $this,               // Controller
                        'Projects',            // Source name
                        'Project',            // Source class
                        array('Name' => 'Name', 'Description'=>'Description', 'SiteURL' => 'SiteURL','IsActive' => 'Status', 'IsFeaturedProjectImage' =>'FeaturedProject'),
                        'getProjectCMSFields_forPopup'
                           );
      
      $projectfield->addPermission("duplicate");
      $projectfield->removePermission("delete");
      
      $projectfield->setColumnWidths(array(
      'IsActive' => 10,
      'IsFeaturedProjectImage' => 10,
       'Name'=> 20,
       'Description'=> 40,
       'SiteURL'=> 20
      ));
      
      $projectFields->addFieldToTab("Root.Content.Projects", $projectfield);
      
      
      $tablefield = new DataObjectManager(
       $this,
       'MyClients',
       'Client',
       array(
       'ID' => 'ID',
      'ClientName' => 'Name',
      'ThumbnailLogo' => 'Logo',
      'ThumbnailPhoto' => 'Photo'
      ,'IsActive' => 'Status'
       ),
       'getClientCMSFields_forPopup'
    );
   
   $tablefield->removePermission("delete");

   $projectFields->addFieldToTab( 'Root.Content.Clients', $tablefield );
   
      
      $tablefield = new ComplexTableField(
$this,
'BusinessAreas',
'BusinessArea',
array(
    'Type' => 'Name'
      , 'CategoryThumbnail' => 'Image'
),
'getBusinessAreaCMSFields_forPopup'
);

      $projectFields->addFieldToTab( 'Root.Content.CategoryBusinessArea', $tablefield );
      
      
      $projectFields->addFieldToTab("Root.Content.CategoryEnvironments",
      new HeaderField($title ='These are the environments', $headingLevel = "3"));
      
      
$tablefield = new ComplexTableField(
    $this,
    'Environments',
    'Environment',
    array(
   'Type' => 'Name',
   'CategoryThumbnail' => 'Image'
    ),
    'getEnvironmentCMSFields_forPopup'
);

   $projectFields->addFieldToTab( 'Root.Content.CategoryEnvironments', $tablefield );
   
   $projectFields->addFieldToTab("Root.Content.CategoryIndustries",
      new HeaderField($title ='These are the industries', $headingLevel = "3"));
      
   
   $tablefield = new ComplexTableField(
    $this,
    'MyIndustrys',
    'Industry',
    array(
    'ID' => 'ID',
   'Type' => 'Name',
   'CategoryThumbnail' => 'Image'
    ),
    'getIndustryCMSFields_forPopup'
);

   $projectFields->addFieldToTab( 'Root.Content.CategoryIndustries', $tablefield );

      $projectFields->addFieldToTab("Root.Content.CategoryTechnologies",
      new HeaderField($title ='These are the Technologies', $headingLevel = "3"));
      

      $tablefield = new DataObjectManager(
       $this,
       'MyTechnologys',
       'Technology',
       array(
      'Type' => 'Name',
      'CategoryThumbnail' => 'Image'
      ),
       'getTechnologyCMSFields_forPopup'
    );
   
    $tablefield->removePermission("delete");
   
   $projectFields->addFieldToTab( 'Root.Content.CategoryTechnologies', $tablefield );
   

      return $projectFields;
   }

}

class Project extends DataObject
{
   static $db = array (
      'Name' => 'Text',
      'Description' => 'Text',
      'Goal' => 'Text',
      'Approach' => 'Text',
      'Results' => 'Text',
      'SiteURL' => 'HTMLText', //HTMLText for link
      'Status' => 'Text'
      , 'URLSegment'   => 'Text'
      , 'IsFeaturedProject' => 'Boolean'
   );

static $many_many = array(
   'MyBusinessAreas' => 'BusinessArea'
   , 'MyEnvironments' => 'Environment'
   , 'MyIndustrys' => 'Industry'
   , 'MyTechnologys' => 'Technology'
);
   
   static $has_one = array (
      'ProjectPage' => 'ProjectPage'
      , 'MyClient' => 'Client'
   );
   
    static $has_many = array(
'ProjectImages' => 'ProjectImage'
);
   
   public function getProjectCMSFields_forPopup()
   {
      return new FieldSet(
         new DropdownField('MyClientID',
            'Please choose a client',
            Dataobject::get('Client', '', $default_sort = "ClientName ASC")
            ->toDropdownMap("ID", "ClientName")),
         new CheckboxField($name = "IsFeaturedProject",
                $title = "Is Featured Project?"
                ),
         new OptionsetField($name = "Status",
                $title = "Status",
                $source = array(
                   "active" => "Active",
                   "inactive" => "Inactive"
                ),
                "inactive"
               ),
         new TextField('Name'),
         new TextareaField('Description'),
         new TextareaField('Goal'),
         new TextareaField('Approach'),
         new TextareaField('Results'),
         new TextField('SiteURL'),
         

new CheckboxSetField('MyBusinessAreas', 'Please choose a business area',
Dataobject::get('BusinessArea', '', $default_sort = "Type ASC")
->toDropdownMap("ID", "Type")),

new CheckboxSetField('MyEnvironments', 'Please choose an Environment (Platform) area',
Dataobject::get('Environment', '', $default_sort = "Type ASC")
->toDropdownMap("ID", "Type")),
                  
new CheckboxSetField('MyIndustrys',
'Please choose an industry',
Dataobject::get('Industry', '', $default_sort = "Type ASC")
->toDropdownMap("ID", "Type")),
   
         
         new CheckboxSetField('MyTechnologys',
         'Please choose a technology',
         Dataobject::get('Technology', '', $default_sort = "Type ASC")
         ->toDropdownMap("ID", "Type")),

            

         
         new FileDataObjectManager(
             $this, // Controller
             'ProjectImages', // Source name
             'ProjectImage', // Source class
             'Image', // File name on DataObject
             array('ImageTitle' => 'ImageTitle') // Headings
             // Detail fields (function name or FieldSet object)
             // Filter clause
             // Sort clause
             // Join clause
          )
      );
   }

    function onBeforeWrite () {
      parent::onBeforeWrite ();
if($this->Name)
$this->URLSegment = SiteTree::generateURLSegment($this->Name);
      //
}

function IsActive() {
      $thisStatus = $this->Status;
      if ( $thisStatus=='active' ) {
         return '<h4 style="color:green;">Active</h4>';
      } else {
         return '<h4 style="color:red;">InActive</h4>';
      }
   }
      
function IsFeaturedProjectImage() {
      $featured = $this->IsFeaturedProject;
      if ( $featured=='1' ) {
         //return 'Featured';
         return '<img src="/assets/graphics/Featured_project_icon.png" height="20px"/>';
      } else {
         return null;
      }
   }
   
   function SafeSiteURL() {
   $Url = $this->SiteURL;
      if (($Url!='http://www.')&&($Url!='')){
            return true;
         }
      else { return false;   }
   }
   
    public function customDOMActions()
{
      $featuredTitle = $this->IsFeaturedProject ? "Remove Featured" : "Make Featured";
      $featuredImage = $this->IsFeaturedProject ? "disapprove.png" : "approve.png";
      
      $statusTitle = ($this->Status=='active') ? "Make InActive" : "Make Active";
      $statusImage = ($this->Status=='active') ? "disapprove.png" : "approve.png";
      
      
      $deleteImage = "trash.gif";
         
   $collection = new DataObjectSet();
      
    $collection->push(
      new DataObjectManagerAction(
$statusTitle,
"/Project_Controller/status/$this->ID",
"refresh",
"/assets/graphics/$statusImage"
));
   
    $collection->push(
      new DataObjectManagerAction(
$featuredTitle,
"/Project_Controller/feature/$this->ID",
"refresh",
"/assets/graphics/$featuredImage"
));
       
    return $collection;
}

public function toggleFeatured()
{
if($this->getField('IsFeaturedProject') == 1)
$this->setField('IsFeaturedProject',0);
else
$this->setField('IsFeaturedProject',1);
$this->write();
}

public function toggleStatus()
{
if($this->getField('Status') == 'active')
$this->setField('Status', 'inactive');
else
$this->setField('Status', 'active');
$this->write();
}

public function confirmDelete()
{
if($this)
$this->delete();
else
{
       }
}
}

class Project_Controller extends Controller   {

      static $url_handlers = array(
   'feature/$ID' => 'handleFeatured'
      , 'status/$ID' => 'handleStatus'
);

      public function handleFeatured($request)
{
if($Project = DataObject::get_by_id("Project",$request->param('ID'))) {
$Project->toggleFeatured();
}
}

      public function handleStatus($request)
{
if($Project = DataObject::get_by_id("Project",$request->param('ID'))) {
$Project->toggleStatus();
}
}

      
}
?>

Avatar
UncleCheese

4086 Posts

11 December 2009 at 7:20am

Can you post the code for one of the many_many relations that is failing?

Avatar
UncleCheese

4086 Posts

11 December 2009 at 7:45am

It bugs out in ComplexTableField, too, so I'm guessing it's either

1) a bug in Silverstripe, or
2) Much more likely, a bug in your code somewhere, because i don't think a bug like this would get overlooked for so many releases.

Avatar
vancouverWill

Community Member, 121 Posts

11 December 2009 at 8:00am

In my project DataObject there is the function getProjectCMSFields_forPopup() within that the DropdownField works as expected and that calls a dataobject, the first checkboxfield where it is just a boolean value. The CheckboxSetField gets a dataobject the same way as I do with the dropdown field but it only ever saves the second time around I have posted the code below for the environment and industry dataobjects, I haven't bothered to include the others as they are all pretty similar. For sure it could be a bug in my code but nothing is showing up so hard to know where it is, anyway here is the code. Thanks

<?php
class Environment extends DataObject
{
   static $db = array (
      'Type' => 'Text'
      , 'URLSegment'   => 'Text'
      , 'EnvironmentDefinition' => 'Text'
   );

   /* static $has_one = array(
'EnvironmentProjectPage' => 'ProjectPage'
);*/

static $has_one = array(
      'EnvironmentPhoto' => 'Image'
);
   
   static $belongs_many_many = array(
         'Project' =>' Project'
   
   );

   

   
   public function getEnvironmentCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('Type', 'Environment Name')
         ,new TextareaField('EnvironmentDefinition', 'Environment Definition'),
         new ImageField ($name = "EnvironmentPhoto", $title = "Upload your Image",null, null, null,"assets/Uploads/images_env/")         
      );
   }
   
    function onBeforeWrite () {
      parent::onBeforeWrite ();
if($this->Type)
$this->URLSegment = SiteTree::generateURLSegment($this->Type);
      //
}

   function CategoryImage() {
      $Image = $this->EnvironmentPhoto();
      if ( $Image ) {
         return $Image;
      } else {
         return null;
      }
   }
   
   
      function CategoryThumbnail() {
      $Image = $this->EnvironmentPhoto();
      $ThumbNailID = $this->EnvironmentPhotoID;
      if ( $ThumbNailID > 0 ) {
         return $Image->CMSThumbnail();
      } else {
         return '<p>No Image</p>';
      }
   }
}
?>

<?php
class Industry extends DataObject
{
   static $db = array (
      'Type' => 'Text'
      , 'URLSegment'   => 'Text'
   );

   /* static $has_one = array(
'IndustryProjectPage' => 'ProjectPage'
); */

static $has_one = array(
      'IndustryPhoto' => 'Image'
);
   
   /*static $has_many = array(
         'Projects' =>' Project'
   
   );*/

   static $belongs_many_many = array(
         'Project' =>' Project'
   
   );

   
   public function getIndustryCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('Type', 'Industry Name'),
         new ImageField ($name = "IndustryPhoto", $title = "Upload your Image",null, null, null,"assets/Uploads/images_industry/")
      );
   }

   public function showLink()
   {
      //return $this->IndustryProjectPage()->Link('industry/'.$this->IndustryType);
      //return $this->IndustryProjectPage()->Link('industry/'.$this->Type);
   }
   
    function onBeforeWrite () {
      parent::onBeforeWrite ();
if($this->Type)
$this->URLSegment = SiteTree::generateURLSegment($this->Type);
      //
}

   function CategoryImage() {
      $Image = $this->IndustryPhoto();
      if ( $Image ) {
         return $Image;
      } else {
         return null;
      }
   }
   
   
      function CategoryThumbnail() {
      $Image = $this->IndustryPhoto();
      $ThumbNailID = $this->IndustryPhotoID;
      if ( $ThumbNailID > 0 ) {
         return $Image->CMSThumbnail();
      } else {
         return '<p>No Image</p>';
      }
   }
   
   
}
?>

Avatar
UncleCheese

4086 Posts

18 December 2009 at 8:31am

Did you ever make any progress with this? What a weird bug.

Avatar
vancouverWill

Community Member, 121 Posts

18 December 2009 at 11:49am

no I haven't noticed any error messages or anything so couldn't see where the problem was from. not sure where to go with it. I have been just working around it at the moment and saving the object then second time around adding data to the checkboxes then saving.

Go to Top