Entity

Implements \JsonSerializable

Entity encapsulation, for use with CodeIgniter\Model

package

CodeIgniter

Methods

Allows filling in Entity parameters during construction.

__construct(array|null $data = null) 

Arguments

$data

array|null

Magic method to allow retrieval of protected and private class properties either by their name, or through a `getCamelCasedProperty()` method.

__get(string $key) : mixed

Examples:

 $p = $this->my_property
 $p = $this->getMyProperty()
Throws
\Exception

Arguments

$key

string

Response

mixed

Returns true if a property exists names $key, or a getter method exists named like for __get().

__isset(string $key) : boolean

Arguments

$key

string

Response

boolean

Magic method to all protected/private class properties to be easily set, either through a direct access or a `setCamelCasedProperty()` method.

__set(string $key, null $value = null) : $this

Examples:

 $this->my_property = $p;
 $this->setMyProperty() = $p;
Throws
\Exception

Arguments

$key

string

$value

null

Response

$this

Unsets an attribute property.

__unset(string $key) 
Throws
\ReflectionException

Arguments

$key

string

Provides the ability to cast an item as a specific data type.

castAs( $value, string $type) : mixed

Add ? at the beginning of $type (i.e. ?string) to get NULL instead of casting $value if $value === null

Throws
\Exception

Arguments

$value

$type

string

Response

mixed

Cast as JSON

castAsJson(mixed $value, boolean $asArray = false) : mixed
Throws
\CodeIgniter\Exceptions\CastException

Arguments

$value

mixed

$asArray

boolean

Response

mixed

Takes an array of key/value pairs and sets them as class properties, using any `setCamelCasedProperty()` methods that may or may not exist.

fill(array $data = null) : \CodeIgniter\Entity

Arguments

$data

array

Response

\CodeIgniter\Entity

Checks a property to see if it has changed since the entity was created.

hasChanged(string $key = null) : boolean

Or, without a parameter, checks if any properties have changed.

Arguments

$key

string

Response

boolean

Support for json_encode()

jsonSerialize() : array|mixed
Throws
\Exception

Response

array|mixed

Checks the datamap to see if this column name is being mapped, and returns the mapped name, if any, or the original name.

mapProperty(string $key) : mixed|string

Arguments

$key

string

Response

mixed|string

Converts the given string|timestamp|DateTime|Time instance into a \CodeIgniter\I18n\Time object.

mutateDate( $value) : \CodeIgniter\I18n\Time
Throws
\Exception

Arguments

$value

Response

\CodeIgniter\I18n\Time

Set raw data array without any mutations

setAttributes(array $data) : $this

Arguments

$data

array

Response

$this

Ensures our "original" values match the current values.

syncOriginal() : $this

Response

$this

General method that will return all public and protected values of this entity as an array. All values are accessed through the __get() magic method so will have any casts, etc applied to them.

toArray(boolean $onlyChanged = false, boolean $cast = true) : array
Throws
\Exception

Arguments

$onlyChanged

boolean

If true, only return values that have changed since object creation

$cast

boolean

If true, properties will be casted.

Response

array

Returns the raw values of the current attributes.

toRawArray(boolean $onlyChanged = false) : array

Arguments

$onlyChanged

boolean

Response

array

Properties

Maps names used in sets and gets against unique names within the class, allowing independence from database column names.

datamap : 

Example: $datamap = [ 'db_name' => 'class_name' ];

Type(s)

dates

dates : 

Type(s)

Array of field names and the type of value to cast them as when they are accessed.

casts : 

Type(s)

Holds the current values of all class vars.

attributes : array
var

Type(s)

array

Holds original copies of all class vars so we can determine what's actually been changed and not accidentally write nulls where we shouldn't.

original : array
var

Type(s)

array

Holds info whenever properties have to be casted

_cast : boolean
var

Type(s)

boolean