Commit 44ef2299 authored by Bas de Nooijer's avatar Bas de Nooijer

- fixed bug in addXml builder for multivalue fields

- removed duplicate code in ReadOnly and ReadWrite document
- added a new unittest for testing XML building for multivalue fields
parent 3b520517
......@@ -128,10 +128,15 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request
$xml .= '>';
foreach ($doc->getFields() AS $name => $value) {
$xml .= '<field name="' . $name . '"';
$xml .= $this->attrib('boost', $doc->getFieldBoost($name));
$xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8')
. '</field>';
$boost = $doc->getFieldBoost($name);
if(is_array($value))
{
foreach ($value AS $multival) {
$xml .= $this->_buildFieldXml($name, $boost, $multival);
}
} else {
$xml .= $this->_buildFieldXml($name, $boost, $value);
}
}
$xml .= '</doc>';
......@@ -142,6 +147,26 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request
return $xml;
}
/**
* Build XML for a field
*
* Used in the add command
*
* @param string $name
* @param float $boost
* @param mixed $value
* @return string
*/
protected function _buildFieldXml($name, $boost, $value)
{
$xml = '<field name="' . $name . '"';
$xml .= $this->attrib('boost', $boost);
$xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
$xml .= '</field>';
return $xml;
}
/**
* Build XML for a delete command
*
......
......@@ -150,7 +150,7 @@ class Solarium_Document_ReadOnly
*/
public function offsetExists($offset)
{
return isset($this->_fields[$offset]);
return ($this->__get($offset) !== null);
}
/**
......@@ -172,7 +172,7 @@ class Solarium_Document_ReadOnly
*/
public function offsetGet($offset)
{
return isset($this->_fields[$offset]) ? $this->_fields[$offset] : null;
return $this->__get($offset);
}
}
\ No newline at end of file
......@@ -218,15 +218,8 @@ 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
......
......@@ -103,6 +103,19 @@ class Solarium_Client_Request_UpdateTest extends PHPUnit_Framework_TestCase
);
}
public function testBuildAddXmlMultivalueField()
{
$command = new Solarium_Query_Update_Command_Add;
$command->addDocument(new Solarium_Document_ReadWrite(array('id' => array(1,2,3), 'text' => 'test < 123 > test')));
$request = new Solarium_Client_Request_Update($this->_options, $this->_query);
$this->assertEquals(
'<add><doc><field name="id">1</field><field name="id">2</field><field name="id">3</field><field name="text">test &lt; 123 &gt; test</field></doc></add>',
$request->buildAddXml($command)
);
}
public function testBuildAddXmlSingleDocumentWithBoost()
{
$doc = new Solarium_Document_ReadWrite(array('id' => 1));
......
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