Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@ sudo: false

language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0

env:
- DB=MYSQL CORE_RELEASE=3.2

matrix:
include:
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.1
env: DB=MYSQL CORE_RELEASE=4
- php: 5.6
env: DB=PGSQL CORE_RELEASE=3.2
allow_failures:
env: DB=PGSQL CORE_RELEASE=4
- php: 7.0
env: DB=MYSQL CORE_RELEASE=4
- php: 7.1
env: DB=MYSQL CORE_RELEASE=4

before_script:
- composer self-update || true
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"autoload": {
"psr-4": {
"SilverStripe\\MultiValueField\\": "src/"
"SilverStripe\\MultiValueField\\": "src/",
"SilverStripe\\MultiValueField\\Tests\\": "tests/"
}
}
}
72 changes: 5 additions & 67 deletions src/fields/MultiValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand Down
102 changes: 65 additions & 37 deletions tests/MultiValueFieldTest.php
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']);
Copy link

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.

$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']));
Copy link

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.

$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());
}

}
18 changes: 18 additions & 0 deletions tests/MultiValueFieldTest_DataObject.php
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';

}