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

View File

@ -65,7 +65,7 @@ class CheckboxSetFieldTest extends SapphireTest {
); );
} }
function testSaveWithAssociativeArrayValueSet() { function testSaveWithArrayValueSet() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags'); $article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags'); $articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1'); $tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
@ -99,40 +99,6 @@ class CheckboxSetFieldTest extends SapphireTest {
); );
} }
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() { function testLoadDataFromObject() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags'); $article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags'); $articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');

View File

@ -3,8 +3,17 @@ CheckboxSetFieldTest_Tag:
Title: Tag 1 Title: Tag 1
tag2: tag2:
Title: Tag 2 Title: Tag 2
tag3: CheckboxSetFieldTest_Article:
Title: Tag 3 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: CheckboxSetFieldTest_Article:
articlewithouttags: articlewithouttags:
Content: Article 1 Content: Article 1