PHP Class Spot\Entity

ファイルを表示 Open project: vlucas/spot2 Class Usage Examples

Protected Properties

Property Type Description
$_data array
$_dataModified array
$_errors array Entity error messages (may be present after save attempt)
$_inGetter array
$_inSetter array
$_isNew boolean Entity state
$_objectId string
$mapper boolean
$relationFields array Used internally so entity knows which fields are relations
$table string | null Table name
$tableOptions array Datasource options

Public Methods

Method Description
__construct ( array $data = [] ) Constructor - allows setting of object properties with array on construct
__destruct ( ) Do some cleanup of stored relations so orphaned relations are not held in memory
__get ( string $field ) : boolean | mixed | null Getter for field properties
__isset ( string $key ) : boolean Enable isset() for object properties
__set ( string $field, mixed $value ) Setter for field properties
__toString ( ) : string String representation of the class (JSON)
data ( null | array $data = null, boolean $modified = true, boolean $loadRelations = true ) Gets and sets data on the current entity
dataExcept ( array $except ) : array Return array of field data with data from the field names listed removed
dataModified ( null | string $field = null ) : array | boolean Gets data that has been modified since object construct, optionally allowing for selecting a single field
dataUnmodified ( null | string $field = null ) : array | boolean Gets data that has not been modified since object construct, optionally allowing for selecting a single field
entity ( ) Helper function so entity can be accessed via relation in a more consistent manner with 'entity()' without any errors (i.e. relation will not error if it already has a loaded entity object - it just returns $this)
error ( string $field, mixed $msg ) Add an error to error messages array
errors ( string | array $msgs = null, boolean $overwrite = true ) : array | boolean | Entity Error message getter/setter
events ( EventEmitter $eventEmitter ) Add events to this entity
fields ( ) : array Return defined fields of the entity
get ( string $field ) : boolean | mixed | null
hasErrors ( string $field = null ) : boolean Check if any errors exist
isModified ( null | string $field = null ) : boolean | null Returns true if a field has been modified.
isNew ( null $new = null ) : boolean Is entity new (unsaved)?
jsonSerialize ( ) JsonSerializable
mapper ( ) : boolean Mapper name getter
primaryKey ( ) : mixed Get the value of the primary key field defined on this entity
primaryKeyField ( ) : string Get primary key field name
relation ( string $relationName, null $relationObj = null ) : boolean | mixed Get/Set relation
relations ( spot\MapperInterface $mapper, spot\EntityInterface $entity ) : array Return defined fields of the entity
scopes ( ) Return scopes defined by this entity. Scopes are called from the Spot\Query object as a sort of in-context dynamic query method
set ( string $field, mixed $value, boolean $modified = true )
table ( string | null $tableName = null ) : string Table name getter/setter
tableOptions ( null | array $tableOpts = null ) : array Datasource options getter/setter
toArray ( ) : array Alias of self::data()

Protected Methods

Method Description
initFields ( ) Set all field values to their defaults or null

Method Details

__construct() public method

Constructor - allows setting of object properties with array on construct
public __construct ( array $data = [] )
$data array

__destruct() public method

Do some cleanup of stored relations so orphaned relations are not held in memory
public __destruct ( )

__get() public method

Getter for field properties
public __get ( string $field ) : boolean | mixed | null
$field string
return boolean | mixed | null

__isset() public method

Enable isset() for object properties
public __isset ( string $key ) : boolean
$key string
return boolean

__set() public method

Setter for field properties
public __set ( string $field, mixed $value )
$field string
$value mixed

__toString() public method

String representation of the class (JSON)
public __toString ( ) : string
return string

data() public method

Gets and sets data on the current entity
public data ( null | array $data = null, boolean $modified = true, boolean $loadRelations = true )
$data null | array
$modified boolean
$loadRelations boolean Determine if you want to load entity relations

dataExcept() public method

Return array of field data with data from the field names listed removed
public dataExcept ( array $except ) : array
$except array List of field names to exclude in data list returned
return array

dataModified() public method

Gets data that has been modified since object construct, optionally allowing for selecting a single field
public dataModified ( null | string $field = null ) : array | boolean
$field null | string
return array | boolean

dataUnmodified() public method

Gets data that has not been modified since object construct, optionally allowing for selecting a single field
public dataUnmodified ( null | string $field = null ) : array | boolean
$field null | string
return array | boolean

entity() public method

Helper function so entity can be accessed via relation in a more consistent manner with 'entity()' without any errors (i.e. relation will not error if it already has a loaded entity object - it just returns $this)
public entity ( )

error() public method

Add an error to error messages array
public error ( string $field, mixed $msg )
$field string Field name that error message relates to
$msg mixed Error message text - String or array of messages

errors() public method

Error message getter/setter
public errors ( string | array $msgs = null, boolean $overwrite = true ) : array | boolean | Entity
$msgs string | array string|array String return errors with field key, array sets errors
$overwrite boolean
return array | boolean | Entity Setter return self, getter returns array or boolean if key given and not found

events() public static method

Add events to this entity
public static events ( EventEmitter $eventEmitter )
$eventEmitter EventEmitter

fields() public static method

Return defined fields of the entity
public static fields ( ) : array
return array

get() public method

public get ( string $field ) : boolean | mixed | null
$field string
return boolean | mixed | null

hasErrors() public method

Check if any errors exist
public hasErrors ( string $field = null ) : boolean
$field string OPTIONAL field name
return boolean

initFields() protected method

Set all field values to their defaults or null
protected initFields ( )

isModified() public method

If no field name is passed in, return whether any fields have been changed
public isModified ( null | string $field = null ) : boolean | null
$field null | string
return boolean | null

isNew() public method

Is entity new (unsaved)?
public isNew ( null $new = null ) : boolean
$new null
return boolean

jsonSerialize() public method

JsonSerializable
public jsonSerialize ( )

mapper() public static method

Mapper name getter
public static mapper ( ) : boolean
return boolean

primaryKey() public method

Get the value of the primary key field defined on this entity
public primaryKey ( ) : mixed
return mixed Value of the primary key field

primaryKeyField() public method

Get primary key field name
public primaryKeyField ( ) : string
return string Primary key field name

relation() public method

Get/Set relation
public relation ( string $relationName, null $relationObj = null ) : boolean | mixed
$relationName string
$relationObj null
return boolean | mixed

relations() public static method

Return defined fields of the entity
public static relations ( spot\MapperInterface $mapper, spot\EntityInterface $entity ) : array
$mapper spot\MapperInterface
$entity spot\EntityInterface
return array

scopes() public static method

Return scopes defined by this entity. Scopes are called from the Spot\Query object as a sort of in-context dynamic query method
public static scopes ( )

set() public method

public set ( string $field, mixed $value, boolean $modified = true )
$field string
$value mixed
$modified boolean

table() public static method

Table name getter/setter
public static table ( string | null $tableName = null ) : string
$tableName string | null
return string

tableOptions() public static method

Datasource options getter/setter
public static tableOptions ( null | array $tableOpts = null ) : array
$tableOpts null | array
return array

toArray() public method

Alias of self::data()
public toArray ( ) : array
return array

Property Details

$_data protected_oe property

protected array $_data
return array

$_dataModified protected_oe property

protected array $_dataModified
return array

$_errors protected_oe property

Entity error messages (may be present after save attempt)
protected array $_errors
return array

$_inGetter protected_oe property

protected array $_inGetter
return array

$_inSetter protected_oe property

protected array $_inSetter
return array

$_isNew protected_oe property

Entity state
protected bool $_isNew
return boolean

$_objectId protected_oe property

protected string $_objectId
return string

$mapper protected_oe static_oe property

protected static bool $mapper
return boolean

$relationFields protected_oe static_oe property

Used internally so entity knows which fields are relations
protected static array $relationFields
return array

$table protected_oe static_oe property

Table name
protected static string|null $table
return string | null

$tableOptions protected_oe static_oe property

Datasource options
protected static array $tableOptions
return array