Spot\Mapper::get PHP Method

get() public method

Get a new entity object, or an existing entity from identifiers
public get ( mixed $identifier = false ) : mixed
$identifier mixed Primary key or array of key/values
return mixed Depends on input false If $identifier is scalar and no entity exists
    public function get($identifier = false)
    {
        $entityClass = $this->entity();
        $pkField = $this->primaryKeyField();
        if (false === $identifier) {
            // No parameter passed, create a new empty entity object
            $entity = new $entityClass();
            $entity->data([$pkField => null]);
        } elseif (is_array($identifier)) {
            // An array was passed, create a new entity with that data
            $entity = new $entityClass($identifier);
            $entity->data([$pkField => null]);
        } else {
            // Scalar, find record by primary key
            $entity = $this->first([$pkField => $identifier]);
            if (!$entity) {
                return false;
            }
        }
        // Set default values if entity not loaded
        if (!$this->primaryKey($entity)) {
            $entityDefaultValues = $this->entityManager()->fieldDefaultValues();
            if (count($entityDefaultValues) > 0) {
                $entity->data($entityDefaultValues);
            }
        }
        return $entity;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function findById($speakerId)
 {
     $speaker = $this->mapper->get($speakerId);
     if (false === $speaker) {
         throw new EntityNotFoundException();
     }
     return $speaker;
 }