PHP Class Spot\Mapper

Inheritance: use trait Spot\Di\InjectableTrait
Mostra file Open project: vlucas/spot2 Class Usage Examples

Protected Properties

Property Type Description
$_collectionClass Class Names for required classes - Here so they can be easily overridden
$_hooks Array of hooks
$_queryClass
$entityManager Entity manager
$entityName
$eventEmitter
$locator
$withRelations Temporary relations

Public Methods

Method Description
__construct ( spot\Locator $locator, $entityName ) Constructor Method
all ( ) : Query Find all records
belongsTo ( EntityInterface $entity, $foreignEntity, $localKey ) Relation: BelongsTo
build ( array $data ) : object Get a new entity object, set given data on it
collection ( $cursor, $with = [] ) Create collection from Spot\Query object
collectionClass ( ) : string Get collection class name to use
config ( ) : Config Get config class from locator
connection ( string $connectionName = null ) : Doctrine\DBAL\Connection Get connection to use
connectionIs ( $type ) : boolean Test to see if collection is of the given type
convertToDatabaseValues ( string $entityName, array $data ) : array Prepare data to be dumped to the data store
convertToPHPValues ( string $entityName, array $data ) : array Retrieve data from the data store and map it to PHP values
create ( array $data, array $options = [] ) : object Get a new entity object, set given data on it, and save it
delete ( mixed $conditions = [] ) Delete items matching given conditions
dropTable ( ) Drop/delete table Destructive and dangerous - drops entire data source and all data
entity ( ) : string Get name of the Entity class mapper was instantiated with
entityManager ( ) : Manager Entity manager class for storing information and meta-data about entities
eventEmitter ( ) : Spot\EventEmitter Event emitter for this mapper
exec ( string $sql, array $params = [] ) Execute custom query with no handling - just return affected rows Useful for UPDATE, DELETE, and INSERT queries
fieldExists ( string $field ) Check if field exists in defined fields
fieldInfo ( string $field ) Check if field exists in defined fields
fieldType ( string $field ) : mixed Return field type
fields ( ) : array Get formatted fields with all neccesary array keys and values.
fieldsDefined ( ) : array Get field information exactly how it is defined in the class
first ( array $conditions = [] ) Find first record matching given conditions
get ( mixed $identifier = false ) : mixed Get a new entity object, or an existing entity from identifiers
getMapper ( string $entityName ) : Mapper Get mapper for specified entity
hasMany ( EntityInterface $entity, $entityName, $foreignKey, $localValue = null ) Relation: HasMany
hasManyThrough ( EntityInterface $entity, $hasManyEntity, $throughEntity, $selectField, $whereField ) Relation: HasManyThrough
hasOne ( EntityInterface $entity, $foreignEntity, $foreignKey ) Relation: HasOne
insert ( mixed $entity, array $options = [] ) Insert record
loadEvents ( ) Reset and load Events for mapped entity
loadRelations ( EntityInterface $entity ) Load Relations for mapped entity
migrate ( ) Migrate table structure changes from model to database
prepareEntity ( EntityInterface $entity ) : boolean | null Prepare entity and load necessary objects on it
primaryKey ( EntityInterface $entity ) Get value of primary key for given row result
primaryKeyField ( ) Get value of primary key for given row result
query ( string $sql, array $params = [] ) Find records with custom query
queryBuilder ( ) : Query Begin a new database query - get query builder Acts as a kind of factory to get the current adapter's query builder object
queryClass ( ) : string Get query class name to use
relations ( ) Get defined relations
resolver ( ) Query resolver class for perparing and executing queries, then returning the results
save ( EntityInterface $entity, array $options = [] ) Save record Will update if primary key found, insert if not Performs validation automatically before saving record
saveBelongsToRelations ( EntityInterface $entity, array $options = [] ) : mixed Save related entities that have been assigned or loaded (only BelongsTo relations) See saveHasRelations.
saveHasRelations ( EntityInterface $entity, array $options = [] ) : mixed Save related entities that have been assigned or loaded (only HasOne, HasMany and HasManyThrough relations) See saveBelongsToRelations.
scopes ( ) : array Return scopes defined by this mapper. Scopes are called from the Spot\Query object as a sort of in-context dynamic query method
select ( mixed $fields = "*" ) : Query Begin a new database query - get query builder Acts as a kind of factory to get the current adapter's query builder object
table ( ) : string Get table name
transaction ( Closure $work, $entityName = null ) Transaction with closure
truncateTable ( $cascade = false ) Truncate table Should delete all rows and reset serial/auto_increment keys
update ( EntityInterface $entity, array $options = [] ) Update given entity object
upsert ( array $data, array $where ) : object Upsert save entity - insert or update on duplicate key. Intended to be used in conjunction with fields that are marked 'unique'
validate ( EntityInterface $entity, array $options = [] ) : boolean Run set validation rules on fields
where ( array $conditions = [] ) : Query Find records with given conditions If all parameters are empty, find all records

Protected Methods

Method Description
validateRelatedEntity ( EntityInterface $relatedEntity, EntityInterface $entity, RelationAbstract $relation ) : array Validate related entity if it is new or modified only
validateRelations ( EntityInterface $entity ) : EntityInterface Validate related entities using relations
with ( $collection, $entityName, $with = [] ) Eager-load associations for an entire collection

Method Details

__construct() public method

Constructor Method
public __construct ( spot\Locator $locator, $entityName )
$locator spot\Locator

all() public method

Find all records
public all ( ) : Query
return Query

belongsTo() public method

BelongsTo assumes that the localKey will reference the foreignEntity's primary key. If this is not the case, you probably want to use the 'hasOne' relationship instead.
public belongsTo ( EntityInterface $entity, $foreignEntity, $localKey )
$entity EntityInterface

build() public method

Get a new entity object, set given data on it
public build ( array $data ) : object
$data array array of key/values to set on new Entity instance
return object Instance of $entityClass with $data set on it

collection() public method

Create collection from Spot\Query object
public collection ( $cursor, $with = [] )

collectionClass() public method

Get collection class name to use
public collectionClass ( ) : string
return string

config() public method

Get config class from locator
public config ( ) : Config
return Config

connection() public method

Get connection to use
public connection ( string $connectionName = null ) : Doctrine\DBAL\Connection
$connectionName string Named connection or entity class name
return Doctrine\DBAL\Connection

connectionIs() public method

Test to see if collection is of the given type
public connectionIs ( $type ) : boolean
return boolean

convertToDatabaseValues() public method

Prepare data to be dumped to the data store
public convertToDatabaseValues ( string $entityName, array $data ) : array
$entityName string
$data array Key/value pairs of data to store
return array

convertToPHPValues() public method

Retrieve data from the data store and map it to PHP values
public convertToPHPValues ( string $entityName, array $data ) : array
$entityName string
$data array Key/value pairs of data to store
return array

create() public method

Get a new entity object, set given data on it, and save it
public create ( array $data, array $options = [] ) : object
$data array array of key/values to set on new Entity instance
$options array array of save options that will be passed to insert()
return object Instance of $entityClass with $data set on it

delete() public method

Delete items matching given conditions
public delete ( mixed $conditions = [] )
$conditions mixed Optional array of conditions in column => value pairs

dropTable() public method

Drop/delete table Destructive and dangerous - drops entire data source and all data
public dropTable ( )

entity() public method

Get name of the Entity class mapper was instantiated with
public entity ( ) : string
return string $entityName

entityManager() public method

Entity manager class for storing information and meta-data about entities
public entityManager ( ) : Manager
return Spot\Entity\Manager

eventEmitter() public method

Event emitter for this mapper
public eventEmitter ( ) : Spot\EventEmitter
return Spot\EventEmitter

exec() public method

Execute custom query with no handling - just return affected rows Useful for UPDATE, DELETE, and INSERT queries
public exec ( string $sql, array $params = [] )
$sql string Raw query or SQL to run against the datastore
$params array

fieldExists() public method

Check if field exists in defined fields
public fieldExists ( string $field )
$field string Field name to check for existence

fieldInfo() public method

Check if field exists in defined fields
public fieldInfo ( string $field )
$field string Field name to check for existence

fieldType() public method

Return field type
public fieldType ( string $field ) : mixed
$field string Field name
return mixed Field type string or boolean false

fields() public method

Merges defaults with defined field values to ensure all options exist for each field.
public fields ( ) : array
return array Defined fields plus all defaults for full array of all possible options

fieldsDefined() public method

Get field information exactly how it is defined in the class
public fieldsDefined ( ) : array
return array Defined fields plus all defaults for full array of all possible options

first() public method

Find first record matching given conditions
public first ( array $conditions = [] )
$conditions array Array of conditions in column => value pairs

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

getMapper() public method

Get mapper for specified entity
public getMapper ( string $entityName ) : Mapper
$entityName string Name of Entity object to load mapper for
return Mapper

hasMany() public method

Relation: HasMany
public hasMany ( EntityInterface $entity, $entityName, $foreignKey, $localValue = null )
$entity EntityInterface

hasManyThrough() public method

Relation: HasManyThrough
public hasManyThrough ( EntityInterface $entity, $hasManyEntity, $throughEntity, $selectField, $whereField )
$entity EntityInterface

hasOne() public method

HasOne assumes that the foreignKey will be on the foreignEntity.
public hasOne ( EntityInterface $entity, $foreignEntity, $foreignKey )
$entity EntityInterface

insert() public method

Insert record
public insert ( mixed $entity, array $options = [] )
$entity mixed Entity object or array of field => value pairs
$options array Array of adapter-specific options

loadEvents() public method

Reset and load Events for mapped entity
public loadEvents ( )

loadRelations() public method

Load Relations for mapped entity
public loadRelations ( EntityInterface $entity )
$entity EntityInterface

migrate() public method

Migrate table structure changes from model to database
public migrate ( )

prepareEntity() public method

Prepare entity and load necessary objects on it
public prepareEntity ( EntityInterface $entity ) : boolean | null
$entity EntityInterface
return boolean | null

primaryKey() public method

Get value of primary key for given row result
public primaryKey ( EntityInterface $entity )
$entity EntityInterface Instance of an entity to find the primary key of

primaryKeyField() public method

Get value of primary key for given row result
public primaryKeyField ( )

query() public method

Find records with custom query
public query ( string $sql, array $params = [] )
$sql string Raw query or SQL to run against the datastore
$params array

queryBuilder() public method

Begin a new database query - get query builder Acts as a kind of factory to get the current adapter's query builder object
public queryBuilder ( ) : Query
return Query

queryClass() public method

Get query class name to use
public queryClass ( ) : string
return string

relations() public method

Get defined relations
public relations ( )

resolver() public method

Query resolver class for perparing and executing queries, then returning the results
public resolver ( )

save() public method

Save record Will update if primary key found, insert if not Performs validation automatically before saving record
public save ( EntityInterface $entity, array $options = [] )
$entity EntityInterface Entity object
$options array

saveBelongsToRelations() public method

Save related entities that have been assigned or loaded (only BelongsTo relations) See saveHasRelations.
public saveBelongsToRelations ( EntityInterface $entity, array $options = [] ) : mixed
$entity EntityInterface
$options array
return mixed

saveHasRelations() public method

Save related entities that have been assigned or loaded (only HasOne, HasMany and HasManyThrough relations) See saveBelongsToRelations.
public saveHasRelations ( EntityInterface $entity, array $options = [] ) : mixed
$entity EntityInterface
$options array
return mixed

scopes() public method

Return scopes defined by this mapper. Scopes are called from the Spot\Query object as a sort of in-context dynamic query method
public scopes ( ) : array
return array Array of closures with method name as the key

select() public method

Begin a new database query - get query builder Acts as a kind of factory to get the current adapter's query builder object
public select ( mixed $fields = "*" ) : Query
$fields mixed String for single field or array of fields
return Query

table() public method

Get table name
public table ( ) : string
return string Name of table defined on entity class

transaction() public method

Transaction with closure
public transaction ( Closure $work, $entityName = null )
$work Closure

truncateTable() public method

Truncate table Should delete all rows and reset serial/auto_increment keys
public truncateTable ( $cascade = false )

update() public method

Update given entity object
public update ( EntityInterface $entity, array $options = [] )
$entity EntityInterface Entity object
$options array

upsert() public method

Upsert save entity - insert or update on duplicate key. Intended to be used in conjunction with fields that are marked 'unique'
public upsert ( array $data, array $where ) : object
$data array array of key/values to set on new Entity instance
$where array array of keys to select record by for updating if it already exists
return object Instance of $entityClass with $data set on it

validate() public method

Run set validation rules on fields
public validate ( EntityInterface $entity, array $options = [] ) : boolean
$entity EntityInterface
$options array
return boolean

validateRelatedEntity() protected method

Validate related entity if it is new or modified only
protected validateRelatedEntity ( EntityInterface $relatedEntity, EntityInterface $entity, RelationAbstract $relation ) : array
$relatedEntity EntityInterface
$entity EntityInterface
$relation Spot\Relation\RelationAbstract
return array Related entity errors

validateRelations() protected method

Validate related entities using relations
protected validateRelations ( EntityInterface $entity ) : EntityInterface
$entity EntityInterface
return EntityInterface

where() public method

Find records with given conditions If all parameters are empty, find all records
public where ( array $conditions = [] ) : Query
$conditions array Array of conditions in column => value pairs
return Query

with() protected method

Eager-load associations for an entire collection
protected with ( $collection, $entityName, $with = [] )

Property Details

$_collectionClass protected_oe property

Class Names for required classes - Here so they can be easily overridden
protected $_collectionClass

$_hooks protected_oe property

Array of hooks
protected $_hooks

$_queryClass protected_oe property

protected $_queryClass

$entityManager protected_oe static_oe property

Entity manager
protected static $entityManager

$entityName protected_oe property

protected $entityName

$eventEmitter protected_oe static_oe property

protected static $eventEmitter

$locator protected_oe property

protected $locator

$withRelations protected_oe property

Temporary relations
protected $withRelations