Cake\ElasticSearch\Type::entityClass PHP Method

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
    public function entityClass($name = null)
    {
        if ($name === null && !$this->_documentClass) {
            $default = '\\Cake\\ElasticSearch\\Document';
            $self = get_called_class();
            $parts = explode('\\', $self);
            if ($self === __CLASS__ || count($parts) < 3) {
                return $this->_documentClass = $default;
            }
            $alias = Inflector::singularize(substr(array_pop($parts), 0, -4));
            $name = implode('\\', array_slice($parts, 0, -1)) . '\\Document\\' . $alias;
            if (!class_exists($name)) {
                return $this->_documentClass = $default;
            }
        }
        if ($name !== null) {
            $class = App::classname($name, 'Model/Document');
            $this->_documentClass = $class;
        }
        if (!$this->_documentClass) {
            throw new \RuntimeException(sprintf('Missing document class "%s"', $class));
        }
        return $this->_documentClass;
    }

Usage Example

 /**
  * Hydrate a single document.
  *
  * ### Options:
  *
  * * fieldList: A whitelist of fields to be assigned to the entity. If not present,
  *   the accessible fields list in the entity will be used.
  * * accessibleFields: A list of fields to allow or deny in entity accessible fields.
  * * associated: A list of embedded documents you want to marshal.
  *
  * @param array $data The data to hydrate.
  * @param array $options List of options
  * @return \Cake\ElasticSearch\Document;
  */
 public function one(array $data, array $options = [])
 {
     $entityClass = $this->type->entityClass();
     $entity = new $entityClass();
     $entity->source($this->type->name());
     $options += ['associated' => []];
     list($data, $options) = $this->_prepareDataAndOptions($data, $options);
     if (isset($options['accessibleFields'])) {
         foreach ((array) $options['accessibleFields'] as $key => $value) {
             $entity->accessible($key, $value);
         }
     }
     $errors = $this->_validate($data, $options, true);
     $entity->errors($errors);
     foreach (array_keys($errors) as $badKey) {
         unset($data[$badKey]);
     }
     foreach ($this->type->embedded() as $embed) {
         $property = $embed->property();
         if (in_array($embed->alias(), $options['associated']) && isset($data[$property])) {
             $data[$property] = $this->newNested($embed, $data[$property]);
         }
     }
     if (!isset($options['fieldList'])) {
         $entity->set($data);
         return $entity;
     }
     foreach ((array) $options['fieldList'] as $field) {
         if (array_key_exists($field, $data)) {
             $entity->set($field, $data[$field]);
         }
     }
     return $entity;
 }