Revert "ENHANCEMENT Support numeric array values in CheckboxSetField (?Field[]=val1&Field[]=val2 instead of ?Field[val1]=1&Field[val2]=1)" - data handling with numeric/associative arrays too clumsy, fixed up ListboxField instead

This reverts commit 8fa266462f.
This commit is contained in:
Ingo Schommer 2012-03-05 10:55:44 +01:00
parent e8ad2c2173
commit 344899ab77
3 changed files with 17 additions and 47 deletions

View File

@ -184,16 +184,10 @@ class CheckboxSetField extends OptionsetField {
$fieldname = $this->name ;
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
$idList = array();
// Works for both <select multiple> style - array(0 => 'val1', 1 => 'val2')
// and <input type="checkbox"> style - array('val1' => true, 'val2' => true).
// The <select multiple> element doesn't allow for individual keys in parameter names.
$valuesInKeys = (ArrayLib::is_associative($this->value));
if($this->value) foreach($this->value as $k => $v) {
if($valuesInKeys) {
if($v) $idList[] = $k;
} else {
$idList[] = $v;
}
if($this->value) foreach($this->value as $id => $bool) {
if($bool) {
$idList[] = $id;
}
}
$record->$fieldname()->setByIDList($idList);
} elseif($fieldname && $record) {
@ -220,6 +214,7 @@ class CheckboxSetField extends OptionsetField {
$filtered[] = str_replace(",", "{comma}", $item);
}
}
return implode(',', $filtered);
}

View File

@ -65,7 +65,7 @@ class CheckboxSetFieldTest extends SapphireTest {
);
}
function testSaveWithAssociativeArrayValueSet() {
function testSaveWithArrayValueSet() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
@ -98,40 +98,6 @@ class CheckboxSetFieldTest extends SapphireTest {
'Data shold be saved into CheckboxSetField manymany relation table on the "left end"'
);
}
function testSaveWithNumericArrayValueSet() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
/* Create a CheckboxSetField with 2 items selected. Note that the array is in the format (key) => (selected) */
$field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
$field->setValue(array(
$tag1->ID,
$tag2->ID
));
/* Saving should work */
$field->saveInto($article);
$this->assertEquals(
array($tag1->ID,$tag2->ID),
DB::query("SELECT \"CheckboxSetFieldTest_TagID\"
FROM \"CheckboxSetFieldTest_Article_Tags\"
WHERE \"CheckboxSetFieldTest_Article_Tags\".\"CheckboxSetFieldTest_ArticleID\" = $article->ID
")->column(),
'Data shold be saved into CheckboxSetField manymany relation table on the "right end"'
);
$this->assertEquals(
array($articleWithTags->ID,$article->ID),
DB::query("SELECT \"CheckboxSetFieldTest_ArticleID\"
FROM \"CheckboxSetFieldTest_Article_Tags\"
WHERE \"CheckboxSetFieldTest_Article_Tags\".\"CheckboxSetFieldTest_TagID\" = $tag1->ID
")->column(),
'Data shold be saved into CheckboxSetField manymany relation table on the "left end"'
);
}
function testLoadDataFromObject() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');

View File

@ -3,8 +3,17 @@ CheckboxSetFieldTest_Tag:
Title: Tag 1
tag2:
Title: Tag 2
tag3:
Title: Tag 3
CheckboxSetFieldTest_Article:
articlewithouttags:
Content: Article 1
articlewithtags:
Content: Article 2
Tags: =>CheckboxSetFieldTest_Tag.tag1,=>CheckboxSetFieldTest_Tag.tag2
CheckboxSetFieldTest_Tag:
tag1:
Title: Tag 1
tag2:
Title: Tag 2
CheckboxSetFieldTest_Article:
articlewithouttags:
Content: Article 1