Commit a71f798c authored by Bas de Nooijer's avatar Bas de Nooijer

Added createDocument method to update query

parent f5ddbee9
...@@ -75,6 +75,7 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly ...@@ -75,6 +75,7 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
* Constructor * Constructor
* *
* @param array $fields * @param array $fields
* @param array $boosts
*/ */
public function __construct($fields = array(), $boosts = array()) public function __construct($fields = array(), $boosts = array())
{ {
......
...@@ -79,6 +79,7 @@ class Solarium_Query_Update extends Solarium_Query ...@@ -79,6 +79,7 @@ class Solarium_Query_Update extends Solarium_Query
protected $_options = array( protected $_options = array(
'handler' => 'update', 'handler' => 'update',
'resultclass' => 'Solarium_Result_Update', 'resultclass' => 'Solarium_Result_Update',
'documentclass' => 'Solarium_Document_ReadWrite',
); );
/** /**
...@@ -359,4 +360,47 @@ class Solarium_Query_Update extends Solarium_Query ...@@ -359,4 +360,47 @@ class Solarium_Query_Update extends Solarium_Query
return $this->add(null, $optimize); return $this->add(null, $optimize);
} }
/**
* Set a custom document class for use in the createDocument method
*
* This class should extend Solarium_Document_ReadWrite or
* at least be compatible with it's interface
*
* @param string $value classname
* @return Solarium_Query
*/
public function setDocumentClass($value)
{
return $this->_setOption('documentclass', $value);
}
/**
* Get the current documentclass option
*
* The value is a classname, not an instance
*
* @return string
*/
public function getDocumentClass()
{
return $this->getOption('documentclass');
}
/**
* Create a document object instance
*
* You can optionally directly supply the fields and boosts
* to get a ready-made document instance for direct use in an add command
*
* @param array $fields
* @param array $boosts
* @return Solarium_Document_ReadWrite
*/
public function createDocument($fields = array(), $boosts = array())
{
$class = $this->getDocumentClass();
return new $class($fields, $boosts);
}
} }
\ No newline at end of file
...@@ -402,4 +402,48 @@ class Solarium_Query_UpdateTest extends PHPUnit_Framework_TestCase ...@@ -402,4 +402,48 @@ class Solarium_Query_UpdateTest extends PHPUnit_Framework_TestCase
$this->_query->createCommand('invalidtype'); $this->_query->createCommand('invalidtype');
} }
public function testSetAndGetDocumentClass()
{
$this->_query->setDocumentClass('MyDocument');
$this->assertEquals('MyDocument', $this->_query->getDocumentClass());
}
public function testCreateDocument()
{
$doc = $this->_query->createDocument();
$this->assertThat($doc, $this->isInstanceOf($this->_query->getDocumentClass()));
}
public function testCreateDocumentWithCustomClass()
{
$this->_query->setDocumentClass('MyCustomDoc');
$doc = $this->_query->createDocument();
$this->assertThat($doc, $this->isInstanceOf('MyCustomDoc'));
}
public function testCreateDocumentWithFieldsAndBoosts()
{
$fields = array('id' => 1, 'name' => 'testname');
$boosts = array('name' => 2.7);
$doc = $this->_query->createDocument($fields, $boosts);
$this->assertThat($doc, $this->isInstanceOf($this->_query->getDocumentClass()));
$this->assertEquals(
$fields,
$doc->getFields()
);
$this->assertEquals(
2.7,
$doc->getFieldBoost('name')
);
}
}
class MyCustomDoc extends Solarium_Document_ReadWrite{
} }
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