Adding BlogPosts

Silverstripe Version: 4.*
Hi,
I know this is a begginer question but… :slight_smile:
I’m adding default BlogPosts during dev/build and I don’t know which tabels and fileds I need to populate in order for the BlogPost to be defined properly.
InitialData.php

$blogpost = new BlogPost();
$blogpost->ParentID = $blog->ID;
$blogpost->PublishDate = date("Y-m-d H:i:s"); // not working
$blogpost->Title = _t(__CLASS__ . '.DEFAULTBLOGCONTENT', 'Blogpost title');
$blogpost->Content = _t( 'SilverStripe\\CMS\\Model\\SiteTree.DEFAULTBLOGCONTENT',
   '<p>Some great content</p>');
$blogpost->write();
$blogpost->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$blogpost->flushCache();

This creates a blog post, but It’s missing db data.

BlogPost::get();

doesn’t return the blogpost. If I edit the post in the admin cms, then the post becomes ok. What do I need to add/set?

This creates a blog post, but It’s missing db data.

What do you mean when you say it “creates a blog post”? If BlogPost::get(); doesn’t return the post, then the post hasn’t really been created. Or at least not saved.

What is InitialData.php? Where is this code being called from? There doesn’t appear to be anything wrong with this code itself (other than where you’ve noted the publish date “isn’t working” - but I’ll ignore that since it doesn’t seem related to your actual question).
My guess is that this code isn’t actually being called.

Also, as a side note, copyVersionToStage() probably should be replaced here with simply publishRecursive() unless you explicitly are intending to avoid some of the events that occur for a normal publish.

2 Likes

Hi,
thank you for your answer. The side note solved the problem. :slight_smile:
changeset and changesetitem DB tables weren’t being populated. And also the publish date was null.
I could see the blogposts in the admin section but BlogPost::get(); didn’t return any posts.

What am I doing?
I created InitialData.php with requireDefaultRecords() function that gets called during initial dev/build. I add default pages, blogs, blogposts, groups, permissions, categories and custom DB tables.