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.

Customising the CMS /

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

select form in CMS


Go to End


10 Posts   3663 Views

Avatar
snaip

Community Member, 181 Posts

15 January 2009 at 4:55am

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 {
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 January 2009 at 5:16am

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.

Avatar
snaip

Community Member, 181 Posts

18 January 2009 at 7:02am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 January 2009 at 8:14am

Edited: 18/01/2009 8:15am

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()));

Avatar
snaip

Community Member, 181 Posts

18 January 2009 at 8:24am

thx

its work now :)

Avatar
snaip

Community Member, 181 Posts

18 January 2009 at 12:13pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 January 2009 at 1:34pm

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.

Avatar
snaip

Community Member, 181 Posts

19 January 2009 at 2:20am

Edited: 19/01/2009 2:55am

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 :)

Go to Top