Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » adding new pages

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

Page: 1
Go to End
Author Topic: 1084 Views
  • Solo
    Avatar
    Community Member
    32 Posts

    adding new pages Link to this post

    Sorry for stupid questions, but i m stuck: I make two pages types, the Hotel.php:

    <?php
    class Hotel extends Page {
       static $db = array(
          'Title' => 'Varchar(50)',
          'Description' => 'Text',
          'Location' => 'Text',
          'Zvezd' => 'Text',
          'Adress' => 'Varchar(100)',
          'Telefone' => 'Varchar(100)',
          'Price' => 'Text',
    );
       static $has_one = array(
       'Image' => 'Hotel_Image',
       'HotelHolder' => 'HotelHolder',
    );
       static $default_parent = 'HotelHolder';
       static $can_be_root = false;
       
       static $singular_name = 'Гостиница';
       static $plural_name = 'Гостинницы';
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new TextAreaField('Description', 'Описание'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('Location', 'Местоположение'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('Zvezd', 'Кол-во звезд'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('Adress', 'Адрес'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('Telefone', 'Телефон'));
          $fields->addFieldToTab('Root.Content.Main', new NumericField('Price', 'Цены'));
          return $fields;
       }
       
    }
       
    class Hotel_Controller extends Page_Controller {

    }

    ?>

    and the hotelHolder.php:

    <?php

    class HotelHolder extends Page {
    static $db = array(
    );
    static $has_one = array(
    );
    static $has_many = array(
       'Hotels' => 'Hotel'
    );
    static $allowed_children = array('Hotel');

    }

    class HotelHolder_Controller extends Page_Controller {

       function addHotelForm(){
    return new Form($this, "addHotelForm",
    new FieldSet(
             new TextField('Title', 'Название'),
    new TextareaField('Description', 'Описание'),
    new TextField('Location', 'Местоположение'),
    new TextField('Zvezd', 'Кол-во звезд'),
             new TextField('Adress', 'Адрес'),
             new TextField('Telefone', 'Телефон'),
             new NumericField('Price', 'Цены'),
             new ImageField('Hotel_Image', 'Фото Гостиннцы')
    ),
    new FieldSet(
    new FormAction("doSubmitHotel", "добавить новую гостинницу")
    ),
             new RequiredFields(
    'Description'
    )
    );
       }

    //Функция обработки формы добавления новой гостинницы
       function doSubmitHotel($data, $form){
          $hotel = new Hotel();
          $form->saveInto($hotel);
          $hotel->HotelCategoryID = $this->dataRecord->ID;
          $hotel->write();
          $form->sessionMessage(
             'Гостинница успешно добавлена!',
             'good'
          );
          Director::redirectBack();
          return;
       }

    }

    ?>

    adding new hotel pages is ok in the cms(backend), but, when i tried to add page through frontend - the new pages puts in the root folder not in hotelHolder as i want. What is wrong with my code? Can somebody help?

  • Solo
    Avatar
    Community Member
    32 Posts

    Re: adding new pages Link to this post

    I found the answer: $hotel->HotelCategoryID = $this->dataRecord->ID; must be $hotel->HotelHolderID = $this->dataRecord->ID

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