Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverstripe table_name not set for class

I'm just learning SilverStripe and I'm having a problem with the lessons on the silverstripe website actually.

I have this class:

namespace SilverStripe\Lessons;

use Page;

class ArticlePage extends Page
{
    private static $can_be_root = false;
    private static $db = [
        'Date' => 'Date',
        'Teaser' => 'Text',
        'Author' => 'Varchar(255)',
    ];

//    public function getCMSFields(){
//        $fields = parent::getCMSFields();
//        $fields = addFieldToTab();
//        return $fields;
//    }

}

Now when I to /dev/build I get this feedback on the build page: enter image description here

So what I did next was actually comment out the declaration of the $db array. Then I got this feedback from the dev/build:

enter image description here

So a few things I'm not sure about. The red note in the first output from dev/build states that you should define a table_name for all namespaced models. It really doesn't state that I have a table_name that is not defined. But the next note under it says that table_name not set for class SilverStripe\Lessons\ArticlePage. So is defining a table_name and "setting" a table_name the same thing in the context of these messages?

Also, the second output indicates that a table_name was defined for SilverStripe\Lessons\ArticlePage The fact that there are "2"s appended to each table_name results from me experimenting before I started this post I believe. i.e. once I saw this behaviour, i repeated the experiment to try and reproduce the behaviour, and hence more tables were created and deleted and thus their names had to be incremented. So Why is ArticlePage table_name not set? Can anybody help me with this issue? Dave.

like image 313
Bucephalus Avatar asked Feb 15 '18 11:02

Bucephalus


1 Answers

The message means that you should configure a table_name for your pages (and DataObjects) that will have their own database table.

In your own code, this is best achieved by adding a private static property, like so:

private static $table_name = 'ArticlePage';

To avoid name-clashes, it's a good idea to prefix your table names with your own company name or similar. But this is only really important if you're developing a module or other code that will run in multiple contexts.

The alternative to using the static property would be to define it via YML. So in your _config/mysite.yml you would put:

SilverStripe\Lessons\ArticlePage:
  table_name: ArticlePage

Note: The change with the table_name was introduced after SilverStripe 4.0.0 and therefore the lesson probably didn't cover this topic.

like image 112
bummzack Avatar answered Oct 30 '22 13:10

bummzack