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.

Data Model Questions /

[SOLVED] Problem with Custom Table and DataObjectManager


Reply


1417 Views

Avatar
Breastfed

Community Member, 44 Posts

24 March 2009 at 6:35am

Edited: 24/03/2009 8:02am

Hello Everybody

i have Problems with my Custom Table and the DataObjectManager.
In my current Project is the case Locations. User can Submit a Location, all Locations are listed in a Table with a link to a detailed View.

Structure:
1. Locations (All Locations - listed in Menu)
1.1 Add a Location (Submit - Listed in Menu)
1.2 Location (Detailview - not listed in Menu)

As i read the Polltutorial i created a Submission.php where my Table is set up, now i think doing it that way is wrong and won't work with my DataObjectManager. I think some Relations are messed up or i have 1 File too much.
Anyway... after 2 hours of trying to understand some Tutorials in the Docs i am stuck now and looking for Help.

Here are my PHP Files a Screenshot of my .ss Folder and a Screenshot of the Error i receieve on the Page "Location" in the CMS which is the Detailview.

MySite/Location.php

<?php

   class Location extends Page {
   
      static $has_many = array(
         'Locations' => 'LocationSubmission'
      );
   

      function getCMSFields() {
      
         $fields = parent::getCMSFields();
         $fields->addFieldToTab("Root.Content.Locations", new DataObjectManager(
            $this,
            'Locations',
            'LocationSubmission',
            array('Name' => 'Name'),
            'getCMSFields_forPopup'
         ));

         return $fields;
      
      }

      // Get Location by ID (Detailview)
      function GetLocations()
      {
         return DataObject::get('Location', 'ParentID = {$this->ID}', 'ID DESC');
      }
   }

   
   class Location_Controller extends Page_Controller {
   
      public function GetLocationSubmission(){
         $Params = Director::urlParams(); //This gets our URLparams
         $SubmissionID = $Params['Action']; //gets the ID from our $Params array
         return DataObject::get_by_id("LocationSubmission", "$SubmissionID");
      }

      function ContactForm() {
       // Create fields
       $fields = new FieldSet(
       new TextField('Dein Name'),
       new TextareaField('Deine Nachricht')
       );
      
          // Create actions
      $actions = new FieldSet(
       new FormAction('ContactForm', 'Absenden')
      );
      
      // Create validator
      $validator = new RequiredFields('Name', 'Deine Nachricht');
      
      return new Form($this, 'ContactForm', $fields, $actions, $validator);
      
   }
      
         
   }

?>


MySite/LocationAdd.php

<?php

   class LocationAdd extends Page {
   
   }
   
   
   class LocationAdd_Controller extends Page_Controller {
   
      // Start Form
      function Form() {
      
         // Fields of Location
         $fields = new FieldSet(
            new TextField('Name','Name der Location'),
            new TextField('Author','Name des Ansprechpartners'),
            new EmailField('Email','E-Mail des Ansprechpartners'),
            new TextareaField('Description','Beschreibe deine Location'),
            new TextField('Adress','Adresse'),
            new TextField('PLZ','PLZ'),
            new TextField('Location','Ort'),

            new DropdownField('Country','Land',array(
            #'' => '',
             'Deutschland' => 'Deutschland',
             'Schweiz' => 'Schweiz'
             )
            ),
            
            new CheckboxSetField('Table','Kickertisch(e)',array(
               'Lehmacher'=>'Lehmacher',
               'Tecball'=>'Tecball'
            ))    
                  
         );
         
         $actions = new Fieldset(
            new FormAction('addLocation','Eintragen')
         );
         
         $validator = new RequiredFields(
            'Name',
            'Email',
            'Description'
         );
         
         $form = new Form(
            $this,
            'Form',
            $fields,
            $actions,
            $validator
         
         );
         return $form;
         
      }
      // end Form
   
   
      // Dateneintrag
      function addLocation($data,$form) {
         $location = new LocationSubmission();
         $form->saveInto($location);
         
         $location->write();
      
         $form->sessionMessage(
            'Eingetragen - wird geprŸft',
            'good'
         );
         Director::redirectBack();
         return;
         
      }
   
   
   }
   // end Class

?>


MySite/LocationSubmission.php

<?php
   class LocationSubmission extends DataObject {
      static $db = array(
         'Name' => 'Varchar(200)',
         'Author' => 'Varchar(100)',
         'Email' => 'Varchar (100)',
         'Description' => 'Text',
         'Adress' => 'Varchar(150)',
         'PLZ' => 'Int(5)',
         'Location' => 'Varchar(50)',
         'Country' => 'Varchar(50)',
         'Table' => 'Varchar(150)'

      );
      
      static $has_one = array(
         'LocationSubmission' => 'LocationSubmission'
      );
      
      public function getCMSFields_forPopup()
      {
         return new FieldSet(
            new TextField('Name')

         );
      }
      

   }

   
?>


MySite/LocationHolder.php

<?php
   class LocationHolder extends Page {
      
      static $allowed_children = array(
         'Location'
      );
      
      function LocationShow ()
      {
         $sort = 'Created DESC';
         
         $data = DataObject::get('LocationSubmission','',$sort,'','');
         return $data;
      }
   

   }
   
   class LocationHolder_Controller extends Page_Controller {
   

   
   }
   

?>

This kind of Case will come one more time in that Project and a few times in Future, so now i want to make sure i am doing the right way and looking for help here.

PLease show me what i am doing wrong.

Thank you all!

Attached Files