Jump to:

22975 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » CheckBox Fields in DataObjectManager

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 1655 Views
  • Paco728
    Avatar
    Community Member
    28 Posts

    CheckBox Fields in DataObjectManager Link to this post

    Hello everyone.

    I know how to implement checkbox fields with DataObjectManager. NO HasOneDataObjectManager .. NOT HasManyDataOb EITHER ManyManyDataOb.. because in my case I do not want any relation between objects, in other words to add options to a table not want to be added in the other tables on other pages, but if I want to have their own checks

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    If I'm understanding this correctly you just want a checkbox on a dataobject. All you need to do is a boolean your $db on the dataobject in question.

    static $db=array(
    "myOptionField" => "Boolean",
    )'

    Then in the CMSFieldForPop add a new CheckboxField("myOptionField")

    In the Database it will have a 1 if checked and 0 if not checked

  • Paco728
    Avatar
    Community Member
    28 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    Just not really what I want to do is on my website only to see the records that I select the table (not the popup).
    On the other hand I also thought hasManyDataObjectManager use because it meets the function above I am applying, but the problem is that I add the records in the tables on other pages.

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    Maybe if you post your models it will help to better understand what you're trying to do. From what I understand is you're trying to associate a dataobject with a specific pagetype, and you only want the records associated with that page to render on the page. You may be building your $has_many relation wrong or calling that dataobject incorrectly and thats why its displaying all records on the data object, not those that should be associated with a particular page.

  • Paco728
    Avatar
    Community Member
    28 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    look at the code

    NuevoEquipo.php

    class NuevoEquipo extends NuevaZona {

       static $has_many = array(
        'MiEquipo' => 'Equipos'
       );

    function getCMSFields() {

    $fields = parent::getCMSFields();
       $playersTablefield = new DataObjectManager(
    $this,
    'MiEquipo',
    'Equipos',
    array(
    'Nro_jugad' => 'Nro',
    'Nombre' => 'Apellido y Nombre',
    'PJ_jugad' => 'PJ',
    'PG_jugad' => 'PG',
    'PE_jugad' => 'PE',
    'PP_jugad' => 'PP',
    'Goles' => 'Goles',
    'Prom' => 'Prom. G/P',
    'Tarj_a' => 'Tarj. Amarillas',
    'Tarj_r' => 'Tarj. Rojas'
    ),
    'getCMSFields_forPopup'
    );
    $playersTablefield->setAddTitle( 'jugador' );

    $fields->addFieldToTab( 'Root.Content.Jugadores', $playersTablefield );

    return $fields;
    }

    }

    ..............................................................................................................................................................................................................

    Equipos.php

    class Equipos extends DataObject {

    static $db = array(
    'Nro_jugad' => 'Int',
    'Nombre' => 'text',
    'PJ_jugad' => 'Int',
    'PG_jugad' => 'Int',
    'PE_jugad' => 'Int',
    'PP_jugad' => 'Int',
    'Goles' => 'Int',
    'Prom' => 'Int',
    'Tarj_a' => 'Int',
    'Tarj_r' => 'Int'
    );

    static $default_sort = "Nro_jugad ASC";

    static $has_one = array(
    'Teams' => 'NuevoEquipo'
    );

    function getCMSFields_forPopup() {
    $fields = new FieldSet();
    $fields->push( new NumericField( 'Nro_jugad','Nro' ) );
    $fields->push( new TextField( 'Nombre','Apellido y Nombre' ) );
    $fields->push( new NumericField( 'PJ_jugad','PJ' ) );
    $fields->push( new NumericField( 'PG_jugad','PG' ) );
    $fields->push( new NumericField( 'PE_jugad','PE' ) );
    $fields->push( new NumericField( 'PP_jugad','PP' ) );
    $fields->push( new NumericField( 'Goles' ) );
    $fields->push( new NumericField( 'Prom','Prom. G/P' ) );
    $fields->push( new NumericField( 'Tarj_a','Tarj. Amarillas' ) );
    $fields->push( new NumericField( 'Tarj_r','Tarj. Rojas' ) );
    return $fields;
    }

    }

    As you can see in the picture "team" is a table with the names of players and other data, what I want is that this table only shows what I select (using checkboxfields ) in the table of the cms (see picture cms ).

    Implement dataobjectmanager because otherwise the data will also create in the other teams and I do not want this to happen.
    I'm sorry if I sound so good in English.

    Attached Files
  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    OK See my original post. You need to add a Boolean Field to the Equipos DataObject then either write a new function that only returns records where that field equals 1 or filter it in the template with an <% if myOptionField = 1 %>

    This way the front end will only show selected Equipos

  • Paco728
    Avatar
    Community Member
    28 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    perfectly I understand u, add a Boolean Field to the db and filter it in the template. But how it would look this part?
    how do I make it look in the cms (checkboxfield )?

       $playersTablefield = new DataObjectManager(
    $this,
    'MiEquipo',
    'Equipos',
    array(
    'Nro_jugad' => 'Nro',
    'Nombre' => 'Apellido y Nombre',
    'PJ_jugad' => 'PJ',
    'PG_jugad' => 'PG',
    'PE_jugad' => 'PE',
    'PP_jugad' => 'PP',
    'Goles' => 'Goles',
    'Prom' => 'Prom. G/P',
    'Tarj_a' => 'Tarj. Amarillas',
    'Tarj_r' => 'Tarj. Rojas'   
    ),
    'getCMSFields_forPopup'
    );

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: CheckBox Fields in DataObjectManager Link to this post

    Basicially to call that field in the DOM it would look like

    $playersTablefield = new DataObjectManager(
       $this,
       'MiEquipo',
       'Equipos',
       array(
          'Nro_jugad' => 'Nro',
          'Nombre' => 'Apellido y Nombre',
          'PJ_jugad' => 'PJ',
          'PG_jugad' => 'PG',
          'PE_jugad' => 'PE',
          'PP_jugad' => 'PP',
          'Goles' => 'Goles',
          'Prom' => 'Prom. G/P',
          'Tarj_a' => 'Tarj. Amarillas',
          'Tarj_r' => 'Tarj. Rojas',
          'myOptionField' => 'myOptionField'
       ),
       'getCMSFields_forPopup'
    );

    But you need to define it in the DataObject

    class Equipos extends DataObject {

       static $db = array(
          'Nro_jugad' => 'Int',
          'Nombre' => 'text',
          'PJ_jugad' => 'Int',
          'PG_jugad' => 'Int',
          'PE_jugad' => 'Int',
          'PP_jugad' => 'Int',
          'Goles' => 'Int',
          'Prom' => 'Int',
          'Tarj_a' => 'Int',
          'Tarj_r' => 'Int',
          'myOptionField' => 'Boolean'
       );
       
       static $default_sort = "Nro_jugad ASC";
       
       static $has_one = array(
          
       );

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new CheckboxField( 'myOptionField','Show in Front End' ) );
          $fields->push( new NumericField( 'Nro_jugad','Nro' ) );
          $fields->push( new TextField( 'Nombre','Apellido y Nombre' ) );
          $fields->push( new NumericField( 'PJ_jugad','PJ' ) );
          $fields->push( new NumericField( 'PG_jugad','PG' ) );
          $fields->push( new NumericField( 'PE_jugad','PE' ) );
          $fields->push( new NumericField( 'PP_jugad','PP' ) );
          $fields->push( new NumericField( 'Goles' ) );
          $fields->push( new NumericField( 'Prom','Prom. G/P' ) );
          $fields->push( new NumericField( 'Tarj_a','Tarj. Amarillas' ) );
          $fields->push( new NumericField( 'Tarj_r','Tarj. Rojas' ) );
          return $fields;
       }

    }

    A list of field type can be found in the api documention http://api.silverstripe.org/2.4/forms/core/FormField.html and a list of the different data types here http://doc.silverstripe.org/sapphire/en/topics/data-types

    1655 Views
Page: 1 2
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.