Hi,
I'm trying to migrate a current site into SilverStripe. I read data from the current site from
XML files and wrote a custom task to to save the data to equivalent SilverStripe page types.
One page type I have is the HomePage which lists some of the 'Partners' that work with us. A
HomePage can have many Partners:
class HomePage extends Page {
.
.
.
static $has_many = array(
'PartnersHome' => 'Partner'
);
.
.
.
}
Partners are DataObjects and each has one HomePage:
class Partner extends DataObject {
.
.
.
static $has_one = array (
'HomePage' => 'HomePage'
);
.
.
.
}
In my site migration task, when I process the HomePage, I try to set the Partners
that belong to the HomePage by using the following code:
class DataImporterTask extends BuildTask {
function run() {
.
.
.
$this->processHomePage()
.
.
.
}
function processHomePage() {
$homePage = new HomePage();
.
.
.
$partners = DataObject::get ('Partner');
$homePage->PartnersHome = $partners;
.
.
.
$this->savePage($homePage);
}
function savePage (&$thePage) {
$thePage->writeToStage('Stage');
$thePage->Publish('Stage', 'Live');
$thePage->Status = "Published";
$thePage->flushCache();
# Alternate save method:
# $thePage->write();
# $thePage->doPublish();
# Memory cleanup
$thePage->destroy();
unset($thePage);
}
}
Other fields of the HomePage are saved but the partners are not. I've verified that
there are plenty of records in the Partner table. I've also verified by checking the Partner table which has
a HomePageID field created by the relationship. The HomePageID's are not set (ie, equal to the
default field value of zero).
Is the above approach the correct way to save a relationship manually with the savePage() method?
Or does savePage() only save 'simple' fields and not relationships?
thanks for any advice!