Commit 3b520517 authored by Bas de Nooijer's avatar Bas de Nooijer

- added multivalue support to __set magic method (set field value by property)

- updated ReadWrite unittest
parent af7e6f04
......@@ -112,7 +112,8 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
/**
* Set a field value
*
* If a field already has a value it will be overwritten.
* If a field already has a value it will be overwritten. You cannot use
* this method for a multivalue field.
* If you supply NULL as the value the field will be removed
*
* @param string $key
......@@ -208,6 +209,8 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
* object, by field name.
*
* If you supply NULL as the value the field will be removed
* If you supply an array a multivalue field will be created.
* In all cases any existing (multi)value will be overwritten.
*
* @param string $name
* @param string|null $value
......@@ -215,8 +218,15 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
*/
public function __set($name, $value)
{
if (is_array($value)) {
$this->removeField($name); // remove any existing value(s)
foreach ($value AS $multival) {
$this->addField($name, $multival);
}
} else {
$this->setField($name, $value);
}
}
/**
* Unset field value
......
......@@ -212,6 +212,28 @@ class Solarium_Document_ReadWriteTest extends PHPUnit_Framework_TestCase
);
}
public function testSetAndGetMultivalueFieldByProperty()
{
$values = array('test1', 'test2', 'test3');
$this->_doc->multivaluefield = $values;
$this->assertEquals(
$values,
$this->_doc->multivaluefield
);
}
public function testSetAndGetMultivalueFieldByPropertyOverwrite()
{
$values = array('test1', 'test2', 'test3');
$this->_doc->name = $values;
$this->assertEquals(
$values,
$this->_doc->name
);
}
public function testUnsetFieldByProperty()
{
unset($this->_doc->name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment