PHP Class Cake\ElasticSearch\Type

A type in elastic search is approximately equivalent to a table or collection in a relational datastore. While an index can have multiple types, this ODM maps each type in an index maps to a class.
Inheritance: implements Cake\Datasource\RepositoryInterface, implements Cake\Event\EventDispatcherInterface, use trait Cake\Event\EventManagerTrait, use trait Cake\Datasource\RulesAwareTrait, use trait Cake\Validation\ValidatorAwareTrait
Datei anzeigen Open project: cakephp/elastic-search Class Usage Examples

Protected Properties

Property Type Description
$_connection Cake\ElasticSearch\Datasource\Connection Connection instance
$_documentClass string The name of the class that represent a single document for this type
$_name string The name of the Elastic Search type this class represents
$embeds array Collection of Embedded sub documents this type has.
$schema Cake\ElasticSearch\Datasource\MappingSchema The mapping schema for this type.

Public Methods

Method Description
__construct ( array $config = [] ) Constructor
alias ( string $alias = null ) : string Get the alias for this Type.
callFinder ( string $type, Query $query, array $options = [] ) : Query Calls a finder method directly and applies it to the passed query, if no query is passed a new one will be created and returned
connection ( Connection $conn = null ) : Connection Returns the connection instance or sets a new one
defaultConnectionName ( ) : string The default connection name to inject when creating an instance.
delete ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean Delete a single entity.
deleteAll ( array $conditions ) : boolean Delete all matching records.
embedMany ( string $name, array $options = [] ) : void Mark a property in documents of this type as list of embedded sub-documents.
embedOne ( string $name, array $options = [] ) : void Mark a property in documents of this type as an embedded sub-document.
embedded ( ) : array Get the list of embedded documents this type has.
entityClass ( string $name = null ) : string Returns the class used to hydrate rows for this table or sets a new one
exists ( array $conditions ) : boolean Returns true if there is any record in this repository matching the specified conditions.
find ( string $type = 'all', array $options = [] ) : Query Creates a new Query for this repository and applies some defaults based on the type of search that was selected.
findAll ( Query $query, array $options = [] ) : Query Returns the query as passed
get ( string $primaryKey, array $options = [] ) : Document
getEventManager ( ) : Cake\Event\EventManager Get the event manager for this Table.
hasField ( string $field ) : boolean Check whether or not a field exists in the mapping.
implementedEvents ( ) : array Get the callbacks this Type is interested in.
initialize ( array $config ) : void Initialize a table instance. Called after the constructor.
marshaller ( ) : Marshaller Get a marshaller for this Type instance.
name ( string $name = null ) : string Returns the type name name or sets a new one
newEntities ( array $data, array $options = [] ) : array Create a list of entities + associated entities from an array.
newEntity ( array | null $data = null, array $options = [] ) : Cake\Datasource\EntityInterface Create a new entity + associated entities from an array.
patchEntities ( array | Traversable $entities, array $data, array $options = [] ) : array Merges each of the elements passed in $data into the entities found in $entities respecting the accessible fields configured on the entities.
patchEntity ( Cake\Datasource\EntityInterface $entity, array $data, array $options = [] ) : Cake\Datasource\EntityInterface Merges the passed $data into $entity respecting the accessible fields configured on the entity. Returns the same entity after being altered.
query ( ) : Query Creates a new Query instance for this repository
save ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : Cake\Datasource\EntityInterface | boolean Persists an entity based on the fields that are marked as dirty and returns the same entity after a successful save or false in case of any error.
schema ( ) : array Get the mapping data from the index type.
table ( string $table = null ) : string Get/set the type/table name for this type.
updateAll ( array $fields, array $conditions ) : void Update all matching records.

Method Details

__construct() public method

### Options - connection The Elastica instance. - name The name of the type. If this isn't set the name will be inferred from the class name. - eventManager Used to inject a specific eventmanager. At the end of the constructor the Model.initialize event will be triggered.
public __construct ( array $config = [] )
$config array The configuration options, see above.

alias() public method

This method is just an alias of name().
public alias ( string $alias = null ) : string
$alias string The new type name
return string

callFinder() public method

Calls a finder method directly and applies it to the passed query, if no query is passed a new one will be created and returned
public callFinder ( string $type, Query $query, array $options = [] ) : Query
$type string name of the finder to be called
$query Query The query object to apply the finder options to
$options array List of options to pass to the finder
return Query

connection() public method

Returns the connection instance or sets a new one
public connection ( Connection $conn = null ) : Connection
$conn Cake\ElasticSearch\Datasource\Connection the new connection instance
return Cake\ElasticSearch\Datasource\Connection

defaultConnectionName() public static method

The default connection name to inject when creating an instance.
public static defaultConnectionName ( ) : string
return string

delete() public method

Deletes an entity and possibly related associations from the database based on the 'dependent' option used when defining the association. Triggers the Model.beforeDelete and Model.afterDelete events.
public delete ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean
$entity Cake\Datasource\EntityInterface The entity to remove.
$options array The options for the delete.
return boolean success

deleteAll() public method

Deletes all records matching the provided conditions. This method will *not* trigger beforeDelete/afterDelete events. If you need those first load a collection of records and delete them.
See also: RepositoryInterface::delete()
public deleteAll ( array $conditions ) : boolean
$conditions array An array of conditions, similar to those used with find()
return boolean Success Returns true if one or more rows are effected.

embedMany() public method

Embedded documents are converted into instances of the named document type. This allows you to attach entity level behavior to subsections of your documents. This method will make a list of embedded documents from the named property.
public embedMany ( string $name, array $options = [] ) : void
$name string The name of the property that contains the embedded document.
$options array The options for the embedded document.
return void

embedOne() public method

Embedded documents are converted into instances of the named document type. This allows you to attach entity level behavior to subsections of your documents.
public embedOne ( string $name, array $options = [] ) : void
$name string The name of the property that contains the embedded document.
$options array The options for the embedded document.
return void

embedded() public method

Get the list of embedded documents this type has.
public embedded ( ) : array
return array

entityClass() public method

Returns the class used to hydrate rows for this table or sets a new one
public entityClass ( string $name = null ) : string
$name string the name of the class to use
return string

exists() public method

Returns true if there is any record in this repository matching the specified conditions.
public exists ( array $conditions ) : boolean
$conditions array list of conditions to pass to the query
return boolean

find() public method

### Model.beforeFind event Each find() will trigger a Model.beforeFind event for all attached listeners. Any listener can set a valid result set using $query
public find ( string $type = 'all', array $options = [] ) : Query
$type string the type of query to perform
$options array An array that will be passed to Query::applyOptions
return Query

findAll() public method

Returns the query as passed
public findAll ( Query $query, array $options = [] ) : Query
$query Query An Elasticsearch query object
$options array An array of options to be used for query logic
return Query

get() public method

public get ( string $primaryKey, array $options = [] ) : Document
$primaryKey string The document's primary key
$options array An array of options
return Document A new Elasticsearch document entity

getEventManager() public method

Get the event manager for this Table.
public getEventManager ( ) : Cake\Event\EventManager
return Cake\Event\EventManager

hasField() public method

Check whether or not a field exists in the mapping.
public hasField ( string $field ) : boolean
$field string The field to check.
return boolean

implementedEvents() public method

By implementing the conventional methods a Type class is assumed to be interested in the related event. Override this method if you need to add non-conventional event listeners. Or if you want you table to listen to non-standard events. The conventional method map is: - Model.beforeMarshal => beforeMarshal - Model.beforeFind => beforeFind - Model.beforeSave => beforeSave - Model.afterSave => afterSave - Model.beforeDelete => beforeDelete - Model.afterDelete => afterDelete - Model.beforeRules => beforeRules - Model.afterRules => afterRules
public implementedEvents ( ) : array
return array

initialize() public method

You can use this method to define embedded documents, define validation and do any other initialization logic you need. public function initialize(array $config) { $this->embedMany('Comments'); }
public initialize ( array $config ) : void
$config array Configuration options passed to the constructor
return void

marshaller() public method

Get a marshaller for this Type instance.
public marshaller ( ) : Marshaller
return Marshaller

name() public method

Returns the type name name or sets a new one
public name ( string $name = null ) : string
$name string the new type name
return string

newEntities() public method

This is most useful when hydrating request data back into entities. For example, in your controller code: $articles = $this->Articles->newEntities($this->request->data()); The hydrated entities can then be iterated and saved.
public newEntities ( array $data, array $options = [] ) : array
$data array The data to build an entity with.
$options array A list of options for the objects hydration.
return array An array of hydrated records.

newEntity() public method

This is most useful when hydrating request data back into entities. For example, in your controller code: $article = $this->Articles->newEntity($this->request->data()); The hydrated entity will correctly do an insert/update based on the primary key data existing in the database when the entity is saved. Until the entity is saved, it will be a detached record.
public newEntity ( array | null $data = null, array $options = [] ) : Cake\Datasource\EntityInterface
$data array | null The data to build an entity with.
$options array A list of options for the object hydration.
return Cake\Datasource\EntityInterface

patchEntities() public method

Merging is done by matching the primary key in each of the elements in $data and $entities. This is most useful when editing a list of existing entities using request data: $article = $this->Articles->patchEntities($articles, $this->request->data());
public patchEntities ( array | Traversable $entities, array $data, array $options = [] ) : array
$entities array | Traversable the entities that will get the data merged in
$data array list of arrays to be merged into the entities
$options array A list of options for the objects hydration.
return array

patchEntity() public method

This is most useful when editing an existing entity using request data: $article = $this->Articles->patchEntity($article, $this->request->data());
public patchEntity ( Cake\Datasource\EntityInterface $entity, array $data, array $options = [] ) : Cake\Datasource\EntityInterface
$entity Cake\Datasource\EntityInterface the entity that will get the data merged in
$data array key value list of fields to be merged into the entity
$options array A list of options for the object hydration.
return Cake\Datasource\EntityInterface

query() public method

Creates a new Query instance for this repository
public query ( ) : Query
return Query

save() public method

Triggers the Model.beforeSave and Model.afterSave events. ## Options - checkRules Defaults to true. Check deletion rules before deleting the record.
public save ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : Cake\Datasource\EntityInterface | boolean
$entity Cake\Datasource\EntityInterface The entity to be saved
$options array An array of options to be used for the event
return Cake\Datasource\EntityInterface | boolean

schema() public method

This will fetch the schema from ElasticSearch the first time this method is called.
public schema ( ) : array
return array

table() public method

Get/set the type/table name for this type.
public table ( string $table = null ) : string
$table string The 'table' name for this type.
return string

updateAll() public method

Sets the $fields to the provided values based on $conditions. This method will *not* trigger beforeSave/afterSave events. If you need those first load a collection of records and update them.
public updateAll ( array $fields, array $conditions ) : void
$fields array A hash of field => new value.
$conditions array An array of conditions, similar to those used with find()
return void

Property Details

$_connection protected_oe property

Connection instance
protected Connection,Cake\ElasticSearch\Datasource $_connection
return Cake\ElasticSearch\Datasource\Connection

$_documentClass protected_oe property

The name of the class that represent a single document for this type
protected string $_documentClass
return string

$_name protected_oe property

The name of the Elastic Search type this class represents
protected string $_name
return string

$embeds protected_oe property

Collection of Embedded sub documents this type has.
protected array $embeds
return array

$schema protected_oe property

The mapping schema for this type.
protected MappingSchema,Cake\ElasticSearch\Datasource $schema
return Cake\ElasticSearch\Datasource\MappingSchema