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

Books and Chapters Tabs Problem


Go to End


2 Posts   1054 Views

Avatar
Dannyl

Community Member, 1 Post

7 October 2015 at 6:20am

Hello

So, been spending a couple of days reading the lessons to try and see how to bend Silverstripe to accomplish what I want for my web site. I'm enjoying it a lot but I'm stuck trying to figure out the follow problem; the site will publish amateur short stories, so I have two DataObjects, one for Books and one for Chapters, since the chapters for the different books are released at different times.

I've made changes to the CMS for that and I have one created a new section BookAdmin, with two tabs; Books and Chapters respectively. All is fine; I can create new books in the Books tab and even list them in the Chapters tab, with one minor exception; when I fill the form on the Chapters tab, the data is not saved to the database.

Books are saved, but not chapters. I imagine that I could have the different AdminModel for the chapters as well but it just felt more natural to have the two, Books and Chapters in the same AdminModel.

Am I trying to do something that inherently cannot work or am I missing something?
I would appreciate more experienced eyes on this and will paste the code below.

Books.php

class Books extends DataObject
{
	
	private static $db = array(
		'Name' => 'Varchar',
		'Tagline' => 'Varchar',
		'Teaser' => 'Text'
		);

	private static $has_many = array(
		'Chapter' => 'Chapters'
		);

	public function getCMSFields()
	{
		$fields = FieldList::create(TabSet::create('Root'));
		$fields->AddFieldsToTab('Root.Books', TextField::create('Name'));
		$fields->AddFieldsToTab('Root.Books', TextField::create('Tagline'));
		$fields->AddFieldsToTab('Root.Books', TextareaField::create('Teaser', 'Short Description'));

		$fields->AddFieldsToTab('Root.Chapters', Dropdownfield::create('BooksID','Books')
			->setSource(Books::get()->map('ID','Name'))
			->setEmptyString('-- Select a Book --')
			);
		$fields->AddFieldsToTab('Root.Chapters', TextField::create('Chapter Number', 'Chapter'));
		$fields->AddFieldsToTab('Root.Chapters', DateField::create('Published', 'Publishing Date')->setConfig('showcalendar', true));
		$fields->AddFieldsToTab('Root.Chapters', TextField::create('Author'));
		$fields->AddFieldsToTab('Root.Chapters', TextareaField::create('Content', 'Story'));

		return $fields;
	}
}

Chapters.php

class Chapters extends DataObject
{
	
	private static $db = array(
		'Chapter Number' => 'Int',
		'Content' => 'Text',
		'Published' => 'Date',
		'Author' => 'Varchar'
		);

	private static $has_one = array(
		'Book' => 'Books'
		);

}

BookAdmin.php

class BookAdmin extends ModelAdmin
{

	private static $url_segment = 'books';

	private static $menu_title = 'Books';
	
	private static $managed_models = array(
		'Books',
		'Chapters'
		);

}

Avatar
helenclarko

Community Member, 166 Posts

7 October 2015 at 9:27am

Hi Dannyl,

I have had issues like this before, usually just missing backets or a "," is missing.
First thing I would look at is your error logs, you could also add /?isDev=1 to your address before hitting save and see if any errors pop up.

I'm not sure, but I believe all variable/database names need to be run together/CamelCase.
So "Chapter Number" would become "ChapterNumber"

I believe your current AddFieldsToTab wont work because 'Chapter Number', 'Content', 'Published' and 'Author' dont exist on that dataobject

You could replace the following:

		$fields->AddFieldsToTab('Root.Chapters', TextField::create('Chapter Number', 'Chapter'));
		$fields->AddFieldsToTab('Root.Chapters', DateField::create('Published', 'Publishing Date')->setConfig('showcalendar', true));
		$fields->AddFieldsToTab('Root.Chapters', TextField::create('Author'));
		$fields->AddFieldsToTab('Root.Chapters', TextareaField::create('Content', 'Story'));

With the following:

		$gridFieldConfig = GridFieldConfig::create()->addComponents(
		  new GridFieldToolbarHeader(),
		  new GridFieldAddNewButton('toolbar-header-right'),
		  new GridFieldSortableHeader(),
		  new GridFieldDataColumns(),
		  new GridFieldPaginator(10),
		  new GridFieldEditButton(),
		  new GridFieldDeleteAction(),
		  new GridFieldDetailForm()
		);

		$gridField = new GridField("Chapter", "Chapter list:", $this->Chapter(), $gridFieldConfig);
		$fields->addFieldToTab("Root.Chapters", $gridField);

Possibly?

-helenclarko