Silverstripe Version:
SS4.3 Question:
Struggling to get $Author to display only displaying blank value even though the database is holding a value. Could anybody possibly tell me why I am not able to call this variable from the database? Details of query:
I am new to SilverStripe and have been working through the lessons. I am on lesson 6 “Adding custom fields to a page”. I’ve updated the “ArticlePage” class to generate the fields in the database and use the “FieldList” API to add some new form inputs.
No issues displaying $Date and $Teaser but $Author isn’t working. I’ve checked the GitHub repository to compare my code but unfortunately even though the “Tutor” tells you to use $Author to replace static values he/she hasn’t gone as far as to do the same.
ArticlePage.php
<?php
namespace SilverStripe\Lessons;
use Page;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
class ArticlePage extends Page
{
private static $can_be_root = false;
private static $db = [
'Date' => 'Date',
'Teaser' => 'Text',
'Author' => 'Text',
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', DateField::create('Date','Date of article'),'Content');
$fields->addFieldToTab('Root.Main', TextareaField::create('Teaser')
->setDescription('This is the summary that appears on the article list page.')
,'Content');
$fields->addFieldToTab('Root.Main', TextField::create('Author','Author of article'),'Content');
return $fields;
}
}
Had the same problem… Author value was persisting correctly in the CMS and DB, just resulting in a blank string when trying to use the template var.
Only way I was able to get it working was to not use $Author or $Publisher (note Yoda-Soda was using ‘Publiser’), so presuming they’re reserved in some way, even after implementing @James 's suggestions.
In addition to @JonoM’s answer, just in case, add ?flush to the end of the URL when you view the page. Especially if your site is in dev mode, template caches sometimes get in the way.
I’ve tested my code by including another field to ArticlePage.php to “replace” Author with Publisher. This seemed to work. So I am drawing a conclusion that the field name Author is not allowed to be used. For my sanity sake if anybody has an idea of why this is happening I would be thankful.
private static $db = [
‘Date’ => ‘Date’,
‘Teaser’ => ‘Text’,
‘Author’ => ‘Varchar’,
‘Publiser’ => ‘Varchar’,
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(‘Root.Main’, DateField::create(‘Date’,‘Date of article’), ‘Content’);
$fields->addFieldToTab(‘Root.Main’, TextareaField::create(‘Teaser’)
->setDescription(‘This is the summary that appears on the article list page.’),
‘Content’
);
$fields->addFieldToTab(‘Root.Main’, TextField::create(‘Author’,‘Author of article’),‘Content’);
$fields->addFieldToTab(‘Root.Main’, TextField::create(‘Publiser’,‘Publiser of article’),‘Content’);
I had the exact same issue and finally just gave up. Definitely something up with using ‘Author’ but I couldn’t get to the bottom of it.
Kind of difficult to “hop on the SilverStripe train” when the lessons don’t work as expected and there are thousands of pages of outdated documentation and articles out there. I’m questioning if SilverStripe is the way to go. I want to give it the benefit of the doubt, but struggling daily.
To ensure it’s not a clashing namespacing issue. Try dev/building with:
private static $table_name = 'ArticlePage';
I recall having issues with varchar DB types when not specifying the size. I’m not sure if that’s a PHP7 thing, SilverStripe or maybe just my memory playing up.
I don’t believe it’s a bug as such… If I had to guess, I’d say it’s probably because the Versioned class (which will be applied to any subclass of Page) already has Author() as a method. So trying to get the value of Author is probably invoking the method in that class, rather than getting your page class property via the magic getter.
You could probably test this by adding the following to your page class:
public function getAuthor()
{
return $this->Author;
}
In theory, that would return the property from the class, overriding the method from Versioned… if not, possibly using Author() as the method name… I haven’t tested it!
In any case, you’re probably safer just steering away from using Author as a property to avoid the possible collision.