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.

Data Model Questions /

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

trouble accessing data object via page controlls


Go to End


2 Posts   1788 Views

Avatar
snoopy

Community Member, 4 Posts

31 August 2009 at 6:15am

Hi!

I'm trying to set up a page modifying the example in tutorial 5, but now I got stuck... I'd really appreciate if someone could help me out...

So, I extended the data object like this:

class Teacher extends DataObject {

 static $db = array(
  'FirstName' => 'Text',
  'LastName' => 'Text',
  'Email' => 'Text',
  'PersonalInfo' => 'Text'
  );
  
 static $has_one = array(
  'MyTeacher' => 'TeacherPage',
  'Photo' => 'Image'
 );
 
 function getCMSFields_forPopup() {
  $fields = new FieldSet();
  $fields->push( new TextField('FirstName', 'Vorname'));
  $fields->push( new TextField('LastName', 'Nachname'));
  $fields->push( new TextField('Email', 'Email'));
  $fields->push( new TextAreaField('PersonalInfo', 'Persönliche Informationen'));
  return $fields;
 }
}

Then I created a page in order to contain all this information:

class TeacherPage extends Page {

 static $db = array();

 static $has_many = array(
  'MyTeachers' => 'Teacher'
 );
 	
 function getCMSFields() {
  $fields = parent::getCMSFields();

  $tablefield = new HasManyComplexTableField(
   $this,
   'MyTeachers',
   'Teacher',
   array( 'FirstName' => 'Vorname',
    'LastName' => 'Nachname',
    'Email' => 'Email',
    'PersonalInfo' => 'Persönliche Infos'),
   'getCMSFields_forPopup'
   );
  $tablefield->setAddTitle( 'Lehrer' );
  $fields->addFieldToTab( 'Root.Content.Lehrer', $tablefield);
  return $fields;
 }
}

The cms looks just fine and I can add all the information that I want. But when I'm trying to display all this information, I run into troubles... I don't know how to access the data, correctly... I thought it was supposed to look like this.

  <div id="TeachersList">
   <% if MyTeachers %>
    <% control MyTeachers %>
    <div class="TeacherInfo">
     <h3>$FirstName $LastName</h3>
    </div>
    <% end_control %>
   <% else %>
    nothing found
   <% end_if %>
  </div>

Avatar
Hamish

Community Member, 712 Posts

31 August 2009 at 1:40pm

That all looks fine. What trouble are you having? Ie, what does it display?