Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

CheckBox Fields in DataObjectManager


Go to End


9 Posts   3117 Views

Avatar
Paco728

Community Member, 28 Posts

20 July 2011 at 12:10pm

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

Avatar
zenmonkey

Community Member, 545 Posts

21 July 2011 at 6:18am

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

Avatar
Paco728

Community Member, 28 Posts

21 July 2011 at 6:46pm

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.

Avatar
zenmonkey

Community Member, 545 Posts

22 July 2011 at 1:20am

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.

Avatar
Paco728

Community Member, 28 Posts

22 July 2011 at 5:48am

Edited: 22/07/2011 5:49am

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
Avatar
zenmonkey

Community Member, 545 Posts

22 July 2011 at 6:03am

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

Avatar
Paco728

Community Member, 28 Posts

22 July 2011 at 6:42am

Edited: 22/07/2011 6:44am

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'
);

Avatar
zenmonkey

Community Member, 545 Posts

22 July 2011 at 6:56am

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

Go to Top