Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » static $db variable

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1101 Views
  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    static $db variable Link to this post

    Hi

    I thought of a clever way to define database fields and database forms. I noticed that in writing things like complexTables and various forms on various pages, you have to rewrite a lot of things. That goes against my motto: never write anything twice.

    My question is what can I replace XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX with????? Is there a way to add those from the defining function?

    Basically I want to define the static $db variables using a function.

    <?php

    /*
    DEFINITION of fieldArray
    0 dbfieldname,
    1 dbformat,
    2 formfield
    3 required, 4 show in table list , 5 Ok field,
    */
    class jobSubmissionDataObject extends DataObject {
    var $data;
    static $db = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    static $has_one = array(
    "Parent" => "jobSubPage"
    );

    function __construct() {
    $data = $this->VacancyDefinitions();
    }

    function getCMSFields($withOkfield = 1) {
    $jobsDataObject->CMSFields();
    return $fields;
    }

    function requiredFieldsObject() {
    foreach($this->data as $ds) {
    if($ds[0] && $ds[3]) {
    $fieldsArray[] = $ds[0];
    }
    }
    return new RequiredFields($fieldsArray);
    }

    function complexTableFields() {
    foreach($this->data as $ds) {
    if($ds[0] && $ds[4]) {
    $fieldsArray[$ds[0]] = $ds[0];
    }
    }
    return $fieldsArray;
    }

    private function CMSFields($withOkfield = 1) {
    foreach($this->data as $ds) {
    if(!$ds[5] || $withOkfield) {
    $formFieldArray[] = $ds[2];
    }
    }
    $fieldSet = new FieldSet( $formFieldArray);
    return $fieldSet;
    }

    function databaseFields() {
    $data = $this->VacancyDefinitions();
    foreach($this->data as $ds) {
    if($ds[0] && $ds[1]) {
    $fieldsArray[$ds[0]] = $ds[1];
    }
    }
    return $data;
    }

    /*
    DEFINITION:
    0 dbfieldname,
    1 dbformat,
    2 formfield
    3 required, 4 show in table list , 5 Ok field,
    */

    function VacancyDefinitions() {
    $data = Array (
    Array(
    'Oked',
    'Boolean',
    new CheckboxField('Oked', 'Publish')
    , 0, 1, 0
    ),

    Array(
    '',
    '',
    new HeaderField('Details of person completing this form:', 3, true)
    , 0, 0, 0
    ),
    Array(
    'RequestorName',
    'varchar(255)',
    new TextField('RequestorName', 'Name')
    , 1, 0, 0
    ),
    Array(
    'RequestorPosition',
    'varchar(255)',
    new TextField('RequestorPosition', 'Position')
    , 1, 0, 0
    ),
    Array(
    'RequestorOrganisation',
    'varchar(255)',
    new TextField('RequestorOrganisation', 'Organisation')
    , 1, 0, 0
    ),
    Array(
    'RequestorPhone',
    'varchar(50)',
    new TextField('RequestorPhone', 'Phone')
    , 1, 0, 0
    ),
    Array(
    'RequestorEmail',
    'varchar(255)',
    new EmailField('RequestorEmail', 'Email')
    , 1, 0, 0
    )
    );
    return $data;
    }

    }

    1101 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.