Copied!

Basic CRUD class implementing insert (create), read, update and delete methods.

Classes that derive from \PHPFUI\ORM\Record must define the static members

There is only one record associated with a single CRUD object.

Members of the class can be accessed by their database table field name, case sensitive. On setting a member, it will be cast to the correct PHP type for the field.

Abstract
Constants
public PHPFUI\ORM\Record::ALLOWS_NULL_INDEX = 3
public PHPFUI\ORM\Record::DEFAULT_INDEX = 4
public PHPFUI\ORM\Record::LENGTH_INDEX = 2
public PHPFUI\ORM\Record::MYSQL_TYPE_INDEX = 0
public PHPFUI\ORM\Record::PHP_TYPE_INDEX = 1
Methods
public __construct(PHPFUI\ORM\DataObject|array|string|int|?null $parameter = NULL)
 

Construct a CRUD object

Reads from the database based on the parameters passed to the constructor. No parameters creates an empty object.

Possible $parameter types and values
  • int primary key value, will load object values if the primary key value exists
  • string primary key value, will load object values if the primary key value exists
  • array record is attempted to be read from database using the values of the fields provided.
  • \PHPFUI\ORM\DataObject record is constructed from an existing DataObject
  • null (default) constructs an empty object
  • param int|array<string,mixed>|null|string $parameter
public __get(string $field) : ?mixed
 

Allows for $object->field syntax

Unset fields will return null

public __isset(string $field) : bool
 

Allows for empty($object->field) to work correctly

public __set(string $field, ?mixed $value) : void
public static addDisplayTransform(string $field, callable $callback) : void
 

Add a transform for get. Callback is passed value.

public addSetTransform(string $field, callable $callback) : static
 

Add a transform for set. Callback is passed value.

public blankDate(?string $date) : string
public clean() : static
 

clean is called before insert or update. Override to impliment cleaning on a specific record

public create() : int
 

Alias of insert

public delete() : bool
 

Deletes the record (and children) currently pointed to by the data

  • return bool true if record deleted
public displayTransform(string $field, ?mixed $value = NULL) : ?mixed
 

Transform a field for display

public empty() : bool
 
  • return bool true if empty (default values)
public getAutoIncrement() : bool
 
  • return bool true if table has an auto increment primary key
public static getFields() : array
 
  • return array<string,array> of fields properties indexed by field name
public getLength(string $field) : int
 
  • return int Maximium valid field length
public static getPrimaryKeys() : array
 
  • return string[] primary keys
public getPrimaryKeyValues() : array
 
  • return array<string,string> indexed by primary keys containing the key value
public static getTableName() : string
 
  • return string table name, case sensitive
public static getVirtualFields() : array
 

Get the virtual field names

  • return string[]
public insert() : int|bool
 

Inserts current data into table

  • return int|bool inserted id if auto increment, true on insertion if not auto increment or false on error
public insertOrIgnore() : int|bool
 

Inserts current data into table or ignores duplicate key if found

  • return int|bool inserted id if auto increment, true on insertion if not auto increment or false on error
public insertOrUpdate() : int|bool
 

Inserts current data into table or updates if duplicate key

  • return int|bool inserted id if auto increment, true on insertion if not auto increment or false on error
public PHPFUI\ORM\DataObject::isset(string $field) : bool
public loaded() : bool
 
  • return bool true if loaded from the disk
public loadFromSQL(string $sql, array $input = []) : bool
 

Load first from SQL query

  • param array $input
public PHPFUI\ORM\DataObject::offsetExists( $offset) : bool
public offsetGet( $offset) : ?mixed
 

Low level get access to underlying data to implement ArrayAccess

public offsetSet( $offset, $value) : void
 

Low level set access to underlying data to implement ArrayAccess

public PHPFUI\ORM\DataObject::offsetUnset( $offset) : void
public read(array|string|int $fields) : bool
 

Read a record from the db. If more than one match, only the first is loaded.

  • param array<string,mixed>|int|string $fields if int|string, primary key, otherwise a key => value array to match on. Multiple field value pairs are anded into the where clause.
  • return bool true if a record found
public reload() : bool
 

Reload the object from the database. Unsaved fields are discarded.

public save() : int|bool
 

Save the record, will either update if it exists or insert if not

public setCustomValidator(string $className) : static
 

Set a custom validator class

public setEmpty() : static
 

Sets all fields to default values

public setFrom(array $values, bool $loaded = false) : static
 

Sets the object to values in the array. Invalid array values are ignored.

  • param array<string,mixed> $values
  • param bool $loaded set to true if you want to simulated being loaded from the db.
public PHPFUI\ORM\DataObject::toArray() : array
 
  • return array<string,mixed>
public update() : bool
 

Update the database with the current record based on table primary key

public validate(string $optionalMethod = '', ?self $originalRecord = NULL) : array
 
  • return array<string,string[]> validation errors indexed by offending field containing an array of translated errors
Properties
protected static bool $autoIncrement = false
protected array PHPFUI\ORM\DataObject::$current
protected static bool $deleteChildren = true
protected static array $displayTransforms = []
 
  • var array<string,callable[]>
protected bool $empty = true
protected static array $fields = []
 
  • var array<string,array>
protected bool $loaded = false
protected static array $primaryKeys = []
 
  • var string[]
protected static array $setTransforms = []
 
  • var array<string,callable[]>
protected static string $table = ''
protected string $validator = ''
protected static array $virtualFields = []
 
  • var array<string,string[]>
Methods
protected cleanEmail(string $field) : static
 

Lowercases and strips invalid email characters. Does not validate email address.

protected cleanFloat(string $field, int $decimalPoints = 2) : static
 

removes all non-digits (0-9, . and -)

protected cleanLowerCase(string $field) : static
 

Converts the field to all lower case

protected cleanNumber(string $field) : static
 

removes all non-digits (0-9 and -) from string representation of a number

protected cleanPhone(string $field, string $regExSeparators = '\-\. ') : static
 

removes all invalid characters. (0-9) and regex separators are valid.

protected cleanProperName(string $field) : static
 

Properly capitalizes proper names if in single case. Mixed case strings are not altered.

protected cleanUpperCase(string $field) : static
 

Converts the field to all upper case

protected timeStamp(?int $timeStamp) : string
Methods
private buildWhere(array|string|int $key, array $input) : string
 

Build a where clause

  • param int|array<string,mixed>|string $key if int|string, primary key, otherwise a key => value array of fields to match
  • param array $input
  • return string starting with " where"
private privateInsert(bool $updateOnDuplicate, string $ignore = '') : int|bool
 

Inserts current data into table

  • return int|bool inserted id if auto increment, true on insertion if not auto increment or false on error
private validateFieldExists(string $field) : void
Properties
protected static bool $autoIncrement = false
protected static bool $deleteChildren = true
protected static array $displayTransforms = []
 
  • var array<string,callable[]>
protected static array $fields = []
 
  • var array<string,array>
protected static array $primaryKeys = []
 
  • var string[]
protected static array $setTransforms = []
 
  • var array<string,callable[]>
protected static string $table = ''
protected static array $virtualFields = []
 
  • var array<string,string[]>
Methods
public static addDisplayTransform(string $field, callable $callback) : void
 

Add a transform for get. Callback is passed value.

public static getFields() : array
 
  • return array<string,array> of fields properties indexed by field name
public static getPrimaryKeys() : array
 
  • return string[] primary keys
public static getTableName() : string
 
  • return string table name, case sensitive
public static getVirtualFields() : array
 

Get the virtual field names

  • return string[]
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration