2011-06-07 20:13:34 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Persister;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
2013-06-04 20:45:45 +02:00
|
|
|
use Elastica\Exception\NotFoundException;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
|
2013-04-22 13:20:10 +02:00
|
|
|
use Elastica\Type;
|
|
|
|
use Elastica\Document;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts, replaces and deletes single documents in an elastica type
|
|
|
|
* Accepts domain model objects and converts them to elastica documents
|
|
|
|
*
|
|
|
|
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
|
|
|
|
*/
|
|
|
|
class ObjectPersister implements ObjectPersisterInterface
|
|
|
|
{
|
|
|
|
protected $type;
|
|
|
|
protected $transformer;
|
|
|
|
protected $objectClass;
|
2011-09-12 18:28:59 +02:00
|
|
|
protected $fields;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
|
2011-06-07 20:13:34 +02:00
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
$this->transformer = $transformer;
|
|
|
|
$this->objectClass = $objectClass;
|
2011-09-12 18:28:59 +02:00
|
|
|
$this->fields = $fields;
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert one object into the type
|
|
|
|
* The object will be transformed to an elastica document
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
*/
|
|
|
|
public function insertOne($object)
|
|
|
|
{
|
2012-01-12 20:54:35 +01:00
|
|
|
$document = $this->transformToElasticaDocument($object);
|
|
|
|
$this->type->addDocument($document);
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces one object in the type
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @return null
|
|
|
|
**/
|
|
|
|
public function replaceOne($object)
|
|
|
|
{
|
2012-01-12 20:54:35 +01:00
|
|
|
$document = $this->transformToElasticaDocument($object);
|
2013-06-04 20:45:45 +02:00
|
|
|
try {
|
|
|
|
$this->type->deleteById($document->getId());
|
|
|
|
} catch (NotFoundException $e) {}
|
2012-01-12 20:54:35 +01:00
|
|
|
$this->type->addDocument($document);
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes one object in the type
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @return null
|
|
|
|
**/
|
2012-01-26 10:59:58 +01:00
|
|
|
public function deleteOne($object)
|
2011-06-07 20:13:34 +02:00
|
|
|
{
|
2012-01-26 10:59:58 +01:00
|
|
|
$document = $this->transformToElasticaDocument($object);
|
2013-06-04 20:45:45 +02:00
|
|
|
try {
|
|
|
|
$this->type->deleteById($document->getId());
|
|
|
|
} catch (NotFoundException $e) {}
|
2012-01-25 20:51:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes one object in the type by id
|
|
|
|
*
|
|
|
|
* @param mixed $id
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
**/
|
|
|
|
public function deleteById($id)
|
|
|
|
{
|
2013-06-04 20:45:45 +02:00
|
|
|
try {
|
|
|
|
$this->type->deleteById($id);
|
|
|
|
} catch (NotFoundException $e) {}
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-01 03:14:21 +01:00
|
|
|
* Bulk insert an array of objects in the type for the given method
|
2011-06-07 20:13:34 +02:00
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param array $objects array of domain model objects
|
2014-01-23 17:20:11 +01:00
|
|
|
* @param string Method to call
|
|
|
|
*/
|
2014-02-01 03:14:21 +01:00
|
|
|
public function insertMany(array $objects)
|
2011-06-07 20:13:34 +02:00
|
|
|
{
|
2014-02-01 03:14:21 +01:00
|
|
|
$documents = array();
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$documents[] = $this->transformToElasticaDocument($object);
|
2014-01-23 17:20:11 +01:00
|
|
|
}
|
2014-02-01 03:14:21 +01:00
|
|
|
$this->type->addDocuments($documents);
|
|
|
|
}
|
2014-01-23 17:20:11 +01:00
|
|
|
|
2014-02-01 03:14:21 +01:00
|
|
|
/**
|
|
|
|
* Bulk updates an array of objects in the type
|
|
|
|
*
|
|
|
|
* @param array $objects array of domain model objects
|
|
|
|
*/
|
|
|
|
public function replaceMany(array $objects)
|
|
|
|
{
|
2012-01-12 20:54:35 +01:00
|
|
|
$documents = array();
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$documents[] = $this->transformToElasticaDocument($object);
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
2014-02-01 03:14:21 +01:00
|
|
|
$this->type->updateDocuments($documents);
|
|
|
|
}
|
2014-01-23 17:20:11 +01:00
|
|
|
|
2014-02-01 03:14:21 +01:00
|
|
|
/**
|
|
|
|
* Bulk deletes an array of objects in the type
|
|
|
|
*
|
|
|
|
* @param array $objects array of domain model objects
|
|
|
|
*/
|
|
|
|
public function deleteMany(array $objects)
|
|
|
|
{
|
|
|
|
$documents = array();
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$documents[] = $this->transformToElasticaDocument($object);
|
|
|
|
}
|
|
|
|
$this->type->deleteDocuments($documents);
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms an object to an elastica document
|
|
|
|
*
|
|
|
|
* @param object $object
|
2013-04-22 13:20:10 +02:00
|
|
|
* @return Document the elastica document
|
2011-06-07 20:13:34 +02:00
|
|
|
*/
|
2012-05-29 20:28:20 +02:00
|
|
|
public function transformToElasticaDocument($object)
|
2011-06-07 20:13:34 +02:00
|
|
|
{
|
2011-09-12 18:28:59 +02:00
|
|
|
return $this->transformer->transform($object, $this->fields);
|
2011-06-07 20:13:34 +02:00
|
|
|
}
|
2014-01-23 17:20:11 +01:00
|
|
|
}
|