-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP SS4 compat, rewrite to DBField #44
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,8 @@ | |
namespace SilverStripeAustralia\MultiValueField\Fields; | ||
|
||
use SilverStripe\Core\Convert; | ||
use SilverStripe\ORM\FieldType\DBComposite; | ||
use SilverStripe\ORM\FieldType\DBText; | ||
use SilverStripe\ORM\FieldType\DBVarchar; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use SilverStripe\ORM\DB; | ||
use SilverStripe\ORM\ArrayList; | ||
use SilverStripe\View\ArrayData; | ||
|
||
|
@@ -16,16 +14,8 @@ | |
* | ||
* @author Marcus Nyeholt <[email protected]> | ||
*/ | ||
class MultiValueField extends DBComposite | ||
class MultiValueField extends DBText | ||
{ | ||
protected $changed = false; | ||
|
||
/** | ||
* @param array | ||
*/ | ||
static $composite_db = [ | ||
"Value" => "Text", | ||
]; | ||
|
||
/** | ||
* Returns the value of this field. | ||
|
@@ -34,7 +24,7 @@ class MultiValueField extends DBComposite | |
public function getValue() | ||
{ | ||
// if we're not deserialised yet, do so | ||
if ($this->exists() && is_string($this->value)) { | ||
if (is_string($this->value)) { | ||
$this->value = unserialize($this->value); | ||
} | ||
return $this->value; | ||
|
@@ -67,31 +57,11 @@ public function setName($name) | |
*/ | ||
public function setValue($value, $record = null, $markChanged = true) | ||
{ | ||
if ($markChanged) { | ||
if (is_array($value)) { | ||
$this->value = $value; | ||
$this->changed = true; | ||
} else if (is_object($value)) { | ||
$this->value = isset($value->value) && is_array($value->value) ? $value->value : []; | ||
$this->changed = true; | ||
} else if (!$value) { | ||
$this->value = []; | ||
$this->changed = true; | ||
} | ||
return; | ||
} | ||
|
||
if (!is_array($value) && $record && is_array($record) && isset($record[$this->name.'Value'])) { | ||
$value = $record[$this->name.'Value']; | ||
} | ||
|
||
if ($value && is_string($value)) { | ||
$this->value = unserialize($value); | ||
} else if ($value) { | ||
$this->value = $value; | ||
$value = unserialize($value); | ||
} | ||
|
||
$this->changed = $this->changed || $markChanged; | ||
parent::setValue($value, $record, $markChanged); | ||
} | ||
|
||
/** | ||
|
@@ -113,38 +83,6 @@ public function prepValueForDB($value) | |
} | ||
} | ||
|
||
public function requireField() | ||
{ | ||
$parts= ['datatype'=>'mediumtext', 'character set'=>'utf8', 'collate'=>'utf8_general_ci', 'arrayValue'=>$this->arrayValue]; | ||
$values= ['type'=>'text', 'parts'=>$parts]; | ||
DB::requireField($this->tableName, $this->name . 'Value', $values); | ||
} | ||
|
||
public function compositeDatabaseFields() | ||
{ | ||
return self::$composite_db; | ||
} | ||
|
||
public function writeToManipulation(&$manipulation) | ||
{ | ||
if($this->getValue()) { | ||
$manipulation['fields'][$this->name.'Value'] = $this->prepValueForDB($this->getValue()); | ||
} else { | ||
$manipulation['fields'][$this->name.'Value'] = DBField::create_field('Text', $this->getValue())->nullValue(); | ||
} | ||
} | ||
|
||
public function addToQuery(&$query) | ||
{ | ||
parent::addToQuery($query); | ||
$name = sprintf('%sValue', $this->name); | ||
$val = sprintf('"%sValue"', $this->name); | ||
$select = $query->getSelect(); | ||
if (!isset($select[$name])) { | ||
$query->addSelect([$name => $val]); | ||
} | ||
} | ||
|
||
public function isChanged() | ||
{ | ||
return $this->changed; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,86 @@ | ||
<?php | ||
namespace SilverStripeAustralia\MultiValueField\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use SilverStripeAustralia\MultiValueField\Fields\MultiValueField; | ||
|
||
|
||
/** | ||
* @author Marcus Nyeholt <[email protected]> | ||
*/ | ||
class MultiValueFieldTest extends SapphireTest { | ||
|
||
protected $extraDataObjects = array( | ||
'MultiValueFieldTest_DataObject' | ||
); | ||
protected static $extra_dataobjects = array( | ||
MultiValueFieldTest_DataObject::class | ||
); | ||
|
||
public function testMultiValueField() { | ||
$first = array('One', 'Two', 'Three'); | ||
public function testUpdate() { | ||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->MVField = ['One', 'Two']; | ||
$obj->write(); | ||
|
||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->MVField = $first; | ||
$obj->write(); | ||
$obj = MultiValueFieldTest_DataObject::get()->byID($obj->ID); | ||
$obj->MVField = ['Three']; | ||
$obj->write(); | ||
|
||
$this->assertTrue($obj->isInDB()); | ||
$obj = DataObject::get_by_id('MultiValueFieldTest_DataObject', $obj->ID); | ||
$this->assertEquals(['Three'], $obj->obj('MVField')->getValues()); | ||
} | ||
|
||
$this->assertNotNull($obj->MVField); | ||
$this->assertEquals($first, $obj->MVField->getValues()); | ||
public function testSetArrayAsProperty() { | ||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->MVField = ['One', 'Two']; | ||
$obj->write(); | ||
|
||
$second = array('Four', 'Five'); | ||
$obj->MVField = $second; | ||
$obj->write(); | ||
$obj = MultiValueFieldTest_DataObject::get()->byID($obj->ID); | ||
$this->assertNotNull($obj->MVField); | ||
$this->assertEquals(['One', 'Two'], $obj->obj('MVField')->getValues()); | ||
} | ||
|
||
$this->assertEquals($second, $obj->MVField->getValues()); | ||
} | ||
public function testSetSerialisedStringAsProperty() { | ||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->MVField = serialize(['One', 'Two']); | ||
$obj->write(); | ||
|
||
public function testIsChanged() { | ||
$field = new MultiValueField(); | ||
$this->assertFalse($field->isChanged()); | ||
$obj = MultiValueFieldTest_DataObject::get()->byID($obj->ID); | ||
$this->assertNotNull($obj->MVField); | ||
$this->assertEquals(['One', 'Two'], $obj->obj('MVField')->getValues()); | ||
} | ||
|
||
$field->setValue(array(1, 2, 3)); | ||
$this->assertTrue($field->isChanged()); | ||
public function testSetSerialisedStringWithSetValue() { | ||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->obj('MVField')->setValue(serialize(['One', 'Two'])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update to |
||
$obj->write(); | ||
|
||
$field = new MultiValueField(); | ||
$field->setValue(array(1, 2, 3), null, false); | ||
$this->assertFalse($field->isChanged()); | ||
$obj = MultiValueFieldTest_DataObject::get()->byID($obj->ID); | ||
$this->assertNotNull($obj->MVField); | ||
$this->assertEquals(['One', 'Two'], $obj->obj('MVField')->getValues()); | ||
} | ||
|
||
$field = DBField::create_field('MultiValueField', array(1, 2, 3)); | ||
$field->setValue(null); | ||
$this->assertTrue($field->isChanged()); | ||
} | ||
public function testSetArrayWithSetValue() { | ||
$obj = new MultiValueFieldTest_DataObject(); | ||
$obj->obj('MVField')->setValue(['One', 'Two']); | ||
$obj->write(); | ||
|
||
} | ||
$obj = MultiValueFieldTest_DataObject::get()->byID($obj->ID); | ||
$this->assertNotNull($obj->MVField); | ||
$this->assertEquals(['One', 'Two'], $obj->obj('MVField')->getValues()); | ||
} | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
class MultiValueFieldTest_DataObject extends DataObject implements TestOnly { | ||
public function testIsChanged() { | ||
$field = new MultiValueField(); | ||
$this->assertFalse($field->isChanged()); | ||
|
||
$field->setValue(['One', 'Two']); | ||
$this->assertTrue($field->isChanged()); | ||
|
||
$field = new MultiValueField(); | ||
$field->setValue(['One', 'Two'], null, false); | ||
$this->assertFalse($field->isChanged()); | ||
|
||
private static $db = array( | ||
'MVField' => 'MultiValueField' | ||
); | ||
$field = DBField::create_field('MultiValueField', ['One', 'Two']); | ||
$field->setValue(null); | ||
$this->assertTrue($field->isChanged()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace SilverStripeAustralia\MultiValueField\Tests; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
class MultiValueFieldTest_DataObject extends DataObject implements TestOnly { | ||
|
||
private static $db = array( | ||
'MVField' => 'MultiValueField' | ||
); | ||
|
||
private static $table_name = 'MultiValueFieldTest_DataObject'; | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update to
json_encode()
if prior comments are correct.