Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Site migration with has_many relationship

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 937 Views
  • aragonne
    Avatar
    Community Member
    26 Posts

    Site migration with has_many relationship Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5503 Posts

    Re: Site migration with has_many relationship Link to this post

    $partners = DataObject::get ('Partner');
    $homePage->PartnersHome = $partners;

    Try using the ComponentSet functions.

    $homePage->PartnersHome()->addMany($partners);

  • aragonne
    Avatar
    Community Member
    26 Posts

    Re: Site migration with has_many relationship Link to this post

    Thanks Willr! That did the job.

    Just tacking on additional notes in case anyone is doing something similar. In this example:

    1. The HomePage must already have an ID (ie, it already exists -or- the page must be saved first via $thePage->write() before calling addMany()) so that the home page ID can used to populate the HomePageID foreign key column in the Partner table.

    2. addMany() will take care of adding the correct records to the either the join tables (many_many) or foreign key column of the appropriate table (has_many)

    Reference: "http://doc.silverstripe.org/datamodel?s[]=componentset"

  • aragonne
    Avatar
    Community Member
    26 Posts

    Re: Site migration with has_many relationship Link to this post

    Willr,

    I assume the add() and addMany() methods do not work with 1-to-1 relationships? I tried without success and I resorted having to set the foreign key ID manually. Is the the best approach?

    thanks!

  • Willr
    Avatar
    Forum Moderator
    5503 Posts

    Re: Site migration with has_many relationship Link to this post

    aragonne - correct. ComponentSet's are just for 1-many or many-many releations.

    937 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.