silverstripedata-objects

Silverstripe dataobject to display in multiple pages


I cant seem to figure out how to use a data object in multiple ways. Right now I can only get it to display in one page.

I want to be able to edit the items of the table in the cms, display a list of items on one page and then one specific item on another page.

Here is how I have structured it so far which allows me to list all the clients in a page and edit them in the CMS. I can not list them on a page other than "clientPage" nor can I see a detailed view page of one client.

class Clients extends DataObject {
 public static $db = array(
    //All the table columns
);

 // One-to-one relationship with profile picture
public static $has_one = array(
    'ProfilePicture' => 'Image',
    'ClientPage' => 'ClientPage'
);

// Summary fields

public static $summary_fields = array(
    'ProfilePicture.CMSThumbnail'=>'Picture',
    'FIRST_NAME'=>'First Name',
    'LAST_NAME'=>'Last Name',
    'EMAIL'=>'Email'
);

public function getCMSFields_forPopup() {

    // Profile picture field
    $thumbField = new UploadField('ProfilePicture', 'Profile picture');
    $thumbField->allowedExtensions = array('jpg', 'png', 'gif');


    // Name, Description and Website fields
    return new FieldList(
        //all the editable fields for the cms popup
    );
}
}

The ClientPage

class ClientPage extends Page{
    private static $has_many = array(
      'Clients'=>'Client'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Client', GridField::create(
            'Client',
            'Client List',
            $this->Clients(),
            GridFieldConfig_RecordEditor::create()
        ));

        return $fields;
    }
}

class ClientPage_Controller extends Page_Controller{
    public function init() {
        parent::init();
    }
}

If I try to make a directory page using the same data object it does not work

class ClientDirectoryPage extends Page {
    private static $has_many = array(
      'Clients'=>'Client'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class ClientDirectoryPage_Controller extends Page_Controller{
    public function init() {
        parent::init();
    }
}

Solution

  • Your code does not work because you try to implement Polymorfic has-one relation incorrectly.

    However according to your goal you should have:

    1. A ClientPage that has_one Client (then Client fields are effectively ClientPage fields, as 1-1 relation)
    2. A ClientDirectoryPage displays a collection of links to ClientPages, and relation can be implemented in several ways.

      a. Using SiteTree hierarchy: put several ClientPages under ClientDirectoryPage, and access the list with ClientDirectoryPage::Children()

      b. Get a list of all pages as ClientPage::get() in ClientDirectoryPage_Controller::ClientPages()