Cake\ORM\Table::entityClass PHP Method

entityClass() public method

Returns the class used to hydrate rows for this table or sets a new one
public entityClass ( string | null $name = null ) : string
$name string | null the name of the class to use
return string
    public function entityClass($name = null)
    {
        if ($name === null && !$this->_entityClass) {
            $default = '\\Cake\\ORM\\Entity';
            $self = get_called_class();
            $parts = explode('\\', $self);
            if ($self === __CLASS__ || count($parts) < 3) {
                return $this->_entityClass = $default;
            }
            $alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
            $name = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $alias;
            if (!class_exists($name)) {
                return $this->_entityClass = $default;
            }
        }
        if ($name !== null) {
            $class = App::className($name, 'Model/Entity');
            $this->_entityClass = $class;
        }
        if (!$this->_entityClass) {
            throw new MissingEntityException([$name]);
        }
        return $this->_entityClass;
    }

Usage Example

Beispiel #1
0
 /**
  * Hydrate one entity and its associated data.
  *
  * @param array $data The data to hydrate.
  * @param array $include The associations to include.
  * @return \Cake\ORM\Entity
  * @see \Cake\ORM\Table::newEntity()
  */
 public function one(array $data, array $include = [])
 {
     $propertyMap = $this->_buildPropertyMap($include);
     $schema = $this->_table->schema();
     $tableName = $this->_table->alias();
     $entityClass = $this->_table->entityClass();
     $entity = new $entityClass();
     $entity->source($this->_table->alias());
     if (isset($data[$tableName])) {
         $data = $data[$tableName];
     }
     $properties = [];
     foreach ($data as $key => $value) {
         $columnType = $schema->columnType($key);
         if (isset($propertyMap[$key])) {
             $assoc = $propertyMap[$key]['association'];
             $nested = $propertyMap[$key]['nested'];
             $value = $this->_marshalAssociation($assoc, $value, $nested);
         } elseif ($columnType) {
             $converter = Type::build($columnType);
             $value = $converter->marshal($value);
         }
         $properties[$key] = $value;
     }
     $entity->set($properties);
     return $entity;
 }
All Usage Examples Of Cake\ORM\Table::entityClass