Jump to:

5453 Posts in 1674 Topics by 1198 members

Customising the CMS

SilverStripe Forums » Customising the CMS » select form in CMS

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

Page: 1 2
Go to End
Author Topic: 1421 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    select form in CMS Link to this post

    hi

    i'm still learning silverstripe but the documentation isn't very good for me so i have probably basic question for you

    i want to add a new layer with SELECT FORM with values form database into my CMS

    what is wrong ? i get empty select form

    class TourPage extends Page {

    static $db = array(
    ....
    );

    static $has_one = array(
    ....
    );

    function tour() {
    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array(
    'Title'
    );

    $sqlQuery->from = "SiteTree";

    $sqlQuery->where = "ClassName = TourHolder";

    $rawSQL = $sqlQuery->sql();

    $result = $sqlQuery->execute()->result();
    }

    function getCMSFields() {
    $fields = parent::getCMSFields();

    ....
    ....

    $fields ->addFieldToTab("Root.Content.See Also", new DropdownField('ToursList', 'Tours',$result));

    return $fields;
    }

    }

    class TourPage_Controller extends Page_Controller {
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: select form in CMS Link to this post

    You're working way too hard.

    function getCMSFields()
    {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.See Also", new DropdownField('ToursList','Tours',DataObject::get("Tour")->toDropdownMap()));
    return $fields;
    }

    not sure what this is about:

    function tour() {
    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array(
    'Title'
    );

    Stay away from using the SQLQuery object directly. Stick to the ORM.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: select form in CMS Link to this post

    what is ORM ?

    I want to do this SELECT title FROM tour WHERE ClassName = 'TourPage'
    and result put into dropdownlist in the new tab in CMS

  • UncleCheese
    Avatar
    4085 Posts

    Re: select form in CMS Link to this post

    This didn't work?

    new DropdownField('Tours','Tours',DataObject::get("TourPage")->toDropdownMap());

    I suppose what you really want is to create a relationship to the TourPage.

    static $has_one = ('SeeAlso' => 'TourPage');

    $fields->addFieldToTab("Root.Content.See Also", new DropdownField('SeeAlsoID','Tours',DataObject::get("TourPage")->toDropdownMap()));

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: select form in CMS Link to this post

    thx

    its work now

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: select form in CMS Link to this post

    i need help again :>

    ok i have the dropdownlist and to database a put value which i chose

    static $db = array(
    'SeeAlsoID' => 'Text'
    );

    now i want to select other values form two tables which are connected by SeeAlsoID

    SELECT Title, Content FROM SiteTree, TourPage WHERE SiteTree.ID = TourPage.SeeAlsoID

    next i want to create some template for the query result

    i create function

    class TourPage extends Page {

    static $db = array(
    ...
    );

    static $has_one = array(
    ...
    }

    function tour() {
    ...
    }

    function getCMSFields() {
    ...
    }

       function SeeAlsoData(){
          $sqlQuery = new SQLQuery();
          $sqlQuery->select = array('Title', 'Content');
          $sqlQuery->from = "SiteTree, TourPage";
          $sqlQuery->where = "SiteTree.ID = TourPage.SeeAlsoID";

          // get the raw SQL
          $rawSQL = $sqlQuery->sql();

          // execute and return a Query-object
    $result = new DataObjectSet();
          $result = $sqlQuery->execute();
          foreach($result as $row) {
             $title = $row['Title'];
    $content = $row['Content'];
          }
          return $this->renderWith("SeeAlso");
       }

    }

    in SeeAlso.ss i have

    <% control SeeAlsoData %>
    <p>$title</p>
    <p>$content</p>
    <% end_control %>

    in PageTour.ss i tried include template by $SeeAlso but i get "The website server has not been able to respond to your request."

  • UncleCheese
    Avatar
    4085 Posts

    Re: select form in CMS Link to this post

    Once again, you're working way too hard. You shouldn't be using SQLQuery objects, and actually, none of your code is necessary. It's all built into Silverstripe.

    On TourPage.ss, all you have to do is:

    <% control SeeAlso %>
    $Title
    $Content
    <% end_control %>

    I recommend you read tutorial #5 in the docs on DataObject management.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: select form in CMS Link to this post

    so if i add in TourPage.php this code

    static $has_one = array(
    'SeeAlso' => 'SiteTree'
    );

    i will get relationship between the TourPage table and the SiteTree table where TourPage.ID = SiteTree.ID

    and if i want to get Title from SiteTree only have to do is put this code in TourPage.ss

    <% control SeeAlso %>
    $Title
    <% end_control %>

    ?

    but id doesnt work

    EDIT: it works

    1421 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.