Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject PAge

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

Page: 1 2
Go to End
Author Topic: 4700 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject PAge Link to this post

    What about

    $this->PersonenPages()->First()->URLSegment

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: DataObject PAge Link to this post

    Whoah! You're fast!

    However, no luck

    [Notice] Trying to get property of non-object

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject PAge Link to this post

    Post your code for the Page and the DataObject.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: DataObject PAge Link to this post

    Code of the Page:

    <?php

    class PersonenPage extends Page {
       static $many_many = array(
          'PersonenObjectStandaard' => 'PersonenObject',
          'PersonenObjectUitgelicht' => 'PersonenObject'
       );
       public function getCMSFields() {
          $PersonenObjectStandaard = new ManyManyDataObjectManager (
             $this,
             'PersonenObjectStandaard',
             'PersonenObject',
             array(
                'Name'      => 'Naam',
                'Function'   => 'Functie',
                'Notes'      => 'Opmerkingen'
             ),
             'getCMSFields_forPopup','',
             'Name ASC'
          );
          $PersonenObjectStandaard->setAddTitle('Personen');
          $PersonenObjectStandaard->setSingleTitle('Persoon');
          
          $PersonenObjectUitgelicht = new ManyManyDataObjectManager (
             $this,
             'PersonenObjectUitgelicht',
             'PersonenObject',
             array(
                'Name'      => 'Naam',
                'Function'   => 'Functie',
                'Notes'      => 'Opmerkingen'
             ),
             'getCMSFields_forPopup','',
             'Name ASC'
          );
          $PersonenObjectUitgelicht->setAddTitle('Personen');
          $PersonenObjectUitgelicht->setSingleTitle('Persoon');

          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab("Root.Content.Personen", new TabSet( "Personen",
             new tab('Standaard',new LiteralField('LiF1','<p>Kies hier welke personen weergegeven moeten worden.</p>'), $PersonenObjectStandaard),
             new tab('Uitgelicht',new LiteralField('LiF2',"<p>Kies hier enkele personen die met extra nadruk weergegeven moeten worden (deselecteer deze op het tabblad 'Standaard').</p>"),$PersonenObjectUitgelicht)
          ));
          return $fields;
          }
    }

    class PersonenPage_Controller extends Page_Controller {
          function view($id = null) {
          if($id == null)
             $id = $this->urlParams['ID'];
          if($id && is_numeric($id))
             return DataObject::get_by_id("PersonenObject", $id);
       }
    }
    ?>

    Code of the DataObject:

    <?php

    class PersonenObject extends DataObject{

    static $db = array(
       'Name'      => 'Varchar(50)',
       'Function'   => 'Varchar(50)',
       'Address'   => 'HTMLText',
       'Phone'      => 'Varchar(50)',
       'Email'      => 'Varchar(50)',
       'URL'      => 'Text',
       'Bio'      => 'HTMLText',
       'Notes'      => 'Text',
    );

    static $has_one = array(
       'Image'               => 'Image',
    );
    static $belongs_many_many = array(
       'PersonenPage'         => 'PersonenPage'
    );

    function Thumbnail(){
       $Image = $this->Image();
       if ( $Image ) {
          return $Image->CMSThumbnail();
       } else {
          return null;
       }
    }

    function getCMSFields_forPopup() {
       return new FieldSet(
          new TextField('Name','Naam'),
          new TextField('Function','Functie'),
          new TextField('Address','Adres'),
          new TextField('Phone','Telefoon'),
          new TextField('Email','E-mail (toont een link naar een contactformulier)'),
          new TextField('URL','Website'),
          new SimpleHTMLEditorField('Bio','Biografie',array(
                'css'            => 'mysite/css/jquery.wysiwyg.css',
                'insertOrderedList'   => false,
                'insertImage'      => false,
                'justifyLeft'      => false,
                'justifyRight'      => false,
                'justifyCenter'      => false,
                'cut'            => true,
                'copy'            => true,
                'paste'            => true,
                'html'            => true)
          ),
          new myImageField('Image','Portret'),
          new TextareaField('Notes','Opmerkingen (worden niet weergegeven op de website)')
       );
    }

    function Link() {
       return $this->PersonenPage()->First()->URLSegment . "view/" . $this->ID;
    }

    }
    ?>

  • logix812
    Avatar
    Community Member
    4 Posts

    Re: DataObject PAge Link to this post

    I got this working...

    So I have a holder:
    http://pastebin.com/f5106bdce

    I create a page in the CMS for that holder.

    Now I can:
    http://mysite.com/holder/<any text>/12
    http://mysite.com/holder/<any text>/100

    eg:
    http://mysite.com/holder/view/12
    http://mysite.com/holder/view/100

    http://mysite.com/holder/silverstripe/12
    http://mysite.com/holder/silverstripe/100

    Note the use us static $song in the function song().
    If you don't do that then you will hit the DB for every $song.property in your template.

    I did not modify the Song object at all, eg: Overriding Link()

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: DataObject PAge Link to this post

    Hi!

    I took a look at this and i have a solution which takes the current controller into the url using the SSViewer object.

    I dont know if it is the ideal way but it is a solution i think. Maybe some of the Silverstripe devs can take a look at this?

    My code:

    PersonenPage.php

    <?php

    class PersonenPage extends Page {
    static $many_many = array(
    'PersonenObjectStandaard' => 'PersonenObject',
    'PersonenObjectUitgelicht' => 'PersonenObject'
    );
    public function getCMSFields() {
    $PersonenObjectStandaard = new ManyManyDataObjectManager (
    $this,
    'PersonenObjectStandaard',
    'PersonenObject',
    array(
    'Name' => 'Naam',
    'Function' => 'Functie',
    'Notes' => 'Opmerkingen'
    ),
    'getCMSFields_forPopup','',
    'Name ASC'
    );
    $PersonenObjectStandaard->setAddTitle('Personen');
    $PersonenObjectStandaard->setSingleTitle('Persoon');

    $PersonenObjectUitgelicht = new ManyManyDataObjectManager (
    $this,
    'PersonenObjectUitgelicht',
    'PersonenObject',
    array(
    'Name' => 'Naam',
    'Function' => 'Functie',
    'Notes' => 'Opmerkingen'
    ),
    'getCMSFields_forPopup','',
    'Name ASC'
    );
    $PersonenObjectUitgelicht->setAddTitle('Personen');
    $PersonenObjectUitgelicht->setSingleTitle('Persoon');

    $fields = parent::getCMSFields();

    $fields->addFieldToTab("Root.Content.Personen", new TabSet( "Personen",
    new tab('Standaard',new LiteralField('LiF1','<p>Kies hier welke personen weergegeven moeten worden.</p>'), $PersonenObjectStandaard),
    new tab('Uitgelicht',new LiteralField('LiF2',"<p>Kies hier enkele personen die met extra nadruk weergegeven moeten worden (deselecteer deze op het tabblad 'Standaard').</p>"),$PersonenObjectUitgelicht)
    ));
    return $fields;
    }
    }

    class PersonenPage_Controller extends Page_Controller {

       public function PersonenList() {
          $records = DataObject::get('PersonenObject');   
          return $records;
       }   

       function view($id = null) {
          
       $params = Director::urlParams();
       $id = (int)$params['ID'];

       $object = DataObject::get_by_id('PersonenObject',$id);

       if($object) {
          return $this
             ->customise(array('DisplayPersoon' => $object))
             ->renderWith(array('DisplayPersoon', 'Page'));
       } else {
          Director::redirect(Director::absoluteBaseURL() . 'not-found');
       }
    }    
       
    }
    ?>

    PersonenObject

    <?php

    class PersonenObject extends DataObject{

       static $db = array(
          'Name' => 'Varchar(50)',
          'Function' => 'Varchar(50)',
          'Address' => 'HTMLText',
          'Phone' => 'Varchar(50)',
          'Email' => 'Varchar(50)',
          'URL' => 'Text',
          'Bio' => 'HTMLText',
          'Notes' => 'Text',
       );

       static $has_one = array(
          'Image' => 'Image',
       );

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

       function Thumbnail(){
          $Image = $this->Image();
          if ( $Image ) {
          return $Image->CMSThumbnail();
          } else {
          return null;
          }
       }

       function getCMSFields_forPopup() {
          return new FieldSet(
          new TextField('Name','Naam'),
          new TextField('Function','Functie'),
          new TextField('Address','Adres'),
          new TextField('Phone','Telefoon'),
          new TextField('Email','E-mail (toont een link naar een contactformulier)'),
          new TextField('URL','Website'),
          new SimpleHTMLEditorField('Bio','Biografie',array(
          'css' => 'mysite/css/jquery.wysiwyg.css',
          'insertOrderedList' => false,
          'insertImage' => false,
          'justifyLeft' => false,
          'justifyRight' => false,
          'justifyCenter' => false,
          'cut' => true,
          'copy' => true,
          'paste' => true,
          'html' => true)
          ),
          new ImageField('Image','Portret'),
          new TextareaField('Notes','Opmerkingen (worden niet weergegeven op de website)')
          );
       }

       function Link() {
          return Director::absoluteBaseURL() . SSViewer::topLevel()->URLSegment . "/view/" . $this->ID;
       }

    }
    ?>

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: DataObject PAge Link to this post

    Deleted by poster

    4700 Views
Page: 1 2
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.