Framework  3.9
DataItem Class Reference

DataItem is the generic base class for database mapped classes. More...

+ Inheritance diagram for DataItem:
+ Collaboration diagram for DataItem:

Public Member Functions

 DataItem ()
 Constructor. More...
 
 __sleep ()
 
 fireEvent ($event)
 Fires the specified event to all registered handlers. More...
 
 joinTransaction ($tx)
 Join the DataItem to the specified DataTransaction. More...
 
 getTransaction ()
 Retrieves the current DataTransaction. More...
 
 cast ($class)
 Cast this object to another class. More...
 
 disablePrimaryKey ()
 
 enablePrimaryKey ()
 
 getHiddenFields ()
 Retrieves the list of hidden fields. More...
 
 populate ($line, $alias=false)
 Populates the object using the supplied associative array (field -> value). More...
 
 getFields ()
 Retrieve the field type list for this object. More...
 
 overrideFieldType ($field, $type)
 Override the type for the specified field. More...
 
 getFieldList ($alias="")
 Returned a comma-separated list of the fields for this object (applying the assigned filter if there is one). More...
 
 getFieldArray ()
 Return an array of field names for this object filtered by any active filter. More...
 
 hasField ($field)
 Returns true if this DataItem contains a field with the specified name and that field is not excluded by a filter. More...
 
 hasRelation ($relation)
 Returns true if this DataItem contains a relation with the specified name. More...
 
 getPrimaryKey ()
 Retrieves the primary key field name. More...
 
 getPrimaryKeyList ()
 Retrieves a list of all the primary keys used for an object as an array. More...
 
 get ($field)
 Retrieve the value for the specified field. More...
 
 set ($field, $value)
 Set the value of the specified field. More...
 
 getType ($field)
 Retrieves the data type of the specified field. More...
 
 getFilter ()
 Returns the filter set on this object. More...
 
 setFilter ($filter)
 Sets the filter on this object. More...
 
 getFieldAliases ()
 Retrieve the list of field aliases. More...
 
 getFieldAnnotations ()
 Retrieve the list of field annotations. More...
 
 pack ()
 The pack() method is called prior to persisting a DataItem to storage. More...
 
 unpack ()
 The unpack() method is called after populating a DataItem. More...
 
 load ($id)
 Load the object with the specified primary key. More...
 
 old ()
 Retrieves the currently stored state of the DataItem from the database and returns it as a new object. More...
 
 loadComposite ()
 Creates an outer CompositeDataItem from its base component. More...
 
 save ()
 Store the object in the database. More...
 
 select ()
 Select the object from the database, based on the value of the primary key field. More...
 
 exists ($constraint="")
 Check whether the object exists in the database. More...
 
 update ()
 Update the row in the database that corresponds to this object. More...
 
 updateExplicit ($updates, $params=null)
 Executes an explicit update command against the database. More...
 
 insert ()
 Insert a new row in the database to store this object. More...
 
 delete ($constraint="")
 Delete the row in the database that corresponds to this object. More...
 
 deleteAll ()
 Delete all the rows in the database that correspond to this class. More...
 
 tableExists ()
 Check if the table for this class exists in the database. More...
 
 duplicate ()
 Persists a duplicate of the DataItem to the database under a new primary key. More...
 
 distinctValues ($field, $sorted=false, $constraint="")
 Retrieves the distinct values in the database for the specified field across the specified set of records. More...
 
 fromGET ()
 Automatically populate the object based on parameters in the $_GET collection. More...
 
 fromPOST ()
 Automatically populate the object based on parameters in the $_POST collection. More...
 
 fromREQUEST ()
 Automatically populate the object based on parameters in either the $_GET or $_POST collection, depending on the method used to access the page. More...
 
 fromDataSet ($params)
 Automatically populate the object based on a custom set of parameters (such as a filtered $_POST collection) More...
 
 compare ($to)
 Compare this object to another object. More...
 
 copy ($from)
 Copies values from another object, field by field. More...
 
 cloneFrom ($from)
 Copies values from another object, field by field, excluding the primary key. More...
 
 cacheLookup ($id)
 Populate the object from the local cache if the object is marked as "cacheLocal". More...
 
 getRelated ($class, $field="")
 Returns a single item related by the specified foreign key. More...
 
 getRelatedList ($class, $field="", $orderBy="")
 Returns a list of items that are related to this item. More...
 
 crossReference ($class, $xref, $orderBy="", $pkField="", $xrefField="")
 Returns a list of items related to this item via a cross-reference table. More...
 
 queryValue ($func)
 Query the database to calculate an aggregate value. More...
 
 toXML ($indent=0, $path=null)
 Generates an XML representation of the object. More...
 
 fromXML ($node)
 Populates the object from the specified XML node. More...
 
 toJSON ()
 
 format ($template="", $separator=", ")
 Substitute values for field names in a string, with the fields formatted using their type's default format or according to the formatting template sting passed in. More...
 
 formatField ($template)
 
 formatThroughRelation ($template, $separator=", ")
 
 prettifyFieldName ($field)
 
 relateTo ($target, $field="")
 Link this object to the specified target by setting corresponding field to the value of the target's primary key. More...
 
- Public Member Functions inherited from AbstractDataItem
 decorate ($name, $value)
 Adds a decoration to the DataItem. More...
 
 getDecoration ($name)
 Retrieve the value of a decoration on the current DataItem. More...
 
 setOption ($opt)
 Set a behavior option flag. More...
 
 clearOptions ($opt)
 Clear the specified behavior option flag. More...
 
 getConnection ()
 Retrieves a connection to the database. More...
 
 getIdentityConstraint ()
 Subclasses can override this function to provide additional identity constraints to be used when querying or updating. More...
 
 populate ($line)
 Populates the object using the supplied associative array (field -> value). More...
 
 getPrimaryKeyValue ()
 Retrieves the primary key value. More...
 
 quoteFieldValue ($field, $type=null)
 Returns the properly quoted value of the specified field. More...
 
 quoteValue ($val, $type)
 
 formatFieldValue ($field, $template="")
 
 formatFieldForXML ($field)
 Format field for XML output. More...
 
 reformatToSQLDate ($date)
 Reformats the specified date to be in a format used by the database. More...
 
 reformatFromSQLDate ($date)
 
 fromJSON ($json)
 Populate from the supplied JSON object. More...
 
 prettifyClassName ($plural=false)
 

Static Public Member Functions

static registerEventHandler ($class, $event, $handler)
 Register an event handler for a specific event and DataItem class. More...
 

Public Attributes

 $_tx = null
 
 $_pkDisabled = false
 
- Public Attributes inherited from AbstractDataItem
 $filter
 
 $_options = 0
 
 $_tx = null
 Tracks the current database transaction for each DataItem. More...
 
 $_decorations = null
 

Static Public Attributes

static $_eventMap = array()
 
static $dataTypeRendererMap
 

Detailed Description

DataItem is the generic base class for database mapped classes.

It provides a compact, consistent API for object persistence in the database.

Definition at line 61 of file data_item.inc.

Member Function Documentation

◆ __sleep()

DataItem::__sleep ( )

Definition at line 133 of file data_item.inc.

134  {
135  // No need to persist class variables, such as field metadata
136 
137  $f = array_keys($this->getFields());
138  array_push($f, "primary_key", "table", "filter");
139  return $f;
140  }
getFields()
Retrieve the field type list for this object.
Definition: data_item.inc:333

◆ cacheLookup()

DataItem::cacheLookup (   $id)

Populate the object from the local cache if the object is marked as "cacheLocal".

Reimplemented from AbstractDataItem.

Definition at line 1179 of file data_item.inc.

1180  {
1181  if ($this->cacheLocal)
1182  {
1183  $cache = get_class($this)."_cache";
1184  $items = Cache::get($cache);
1185  if (!is_array($items))
1186  {
1187  $items = IndexedQuery::create(get_class($this), "", $this->primary_key)->execute();
1188  Cache::put($cache, $items);
1189  }
1190  if (array_key_exists($id, $items))
1191  {
1192  trace("Using local cache $cache for item $id", 5);
1193  $this->copy($items[$id]);
1194  return true;
1195  }
1196  }
1197 
1198  return false;
1199  }
static get($key)
Retrieve the specified object from the cache.
Definition: cache.inc:88
static put($key, $obj, $ttl=0)
Store the specified object in the cache at the specified key.
Definition: cache.inc:106
copy($from)
Copies values from another object, field by field.
Definition: data_item.inc:1148
static create($class, $constraints="", $indexBy="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ cast()

DataItem::cast (   $class)

Cast this object to another class.

For simple DataItems this checks that the class is the same as the supplied class and returns a reference if so. If not an exception is thrown.

Parameters
string$class
Returns
instance of the specified class, if compatible
Exceptions
DataItemException

Reimplemented from AbstractDataItem.

Definition at line 219 of file data_item.inc.

220  {
221  if (get_class($this) != $class)
222  {
223  throw new DataItemException("Cannot cast ".get_class($this)." to $class.");
224  }
225 
226  return $this;
227  }

◆ cloneFrom()

DataItem::cloneFrom (   $from)

Copies values from another object, field by field, excluding the primary key.

This works the same as DataItem::copy(), but should be used in preference when you are wanting to create a duplicate row in the database.

Parameters
objectfrom the object from which data is to be copied

Definition at line 1172 of file data_item.inc.

1173  {
1174  $this->copy($from);
1175  $pk = $this->primary_key;
1176  unset($this->$pk);
1177  }

◆ compare()

DataItem::compare (   $to)

Compare this object to another object.

If all the fields match, the method returns true, otherwise false. If a filter is in place on the source object, then only the filtered fields are compared.

Parameters
object$tothe object with which to compare this object

Reimplemented from AbstractDataItem.

Definition at line 1127 of file data_item.inc.

1128  {
1129  foreach($this->getFields() as $field => $type)
1130  {
1131  if ($this->filter && $this->filter->isExcluded($field)) continue;
1132  if ($to->$field != $this->$field)
1133  {
1134  trace("$field '{$this->$field}' != '{$to->$field}'", 3);
1135  return false;
1136  }
1137  }
1138 
1139  return true;
1140  }

◆ copy()

DataItem::copy (   $from)

Copies values from another object, field by field.

If a filter is in place on the source object, then only the filtered fields are copied.

Parameters
objectfrom the object from which data is to be copied

Reimplemented from AbstractDataItem.

Definition at line 1148 of file data_item.inc.

1149  {
1150  foreach($this->getFields() as $field => $type)
1151  {
1152  if ($this->filter && $this->filter->isExcluded($field)) continue;
1153  $this->set($field, $from->$field);
1154  }
1155 
1156  if (is_array($this->calculatedFields))
1157  {
1158  foreach($this->calculatedFields as $field => $expr)
1159  {
1160  $this->set($field, $from->$field);
1161  }
1162  }
1163  }

◆ crossReference()

DataItem::crossReference (   $class,
  $xref,
  $orderBy = "",
  $pkField = "",
  $xrefField = "" 
)

Returns a list of items related to this item via a cross-reference table.

The cross-reference table must contain the primary keys of each of the related classes as fields.

Parameters
string$classthe name of the related class
string$xrefthe name of the cross-reference table
string$orderBy(optional) a sort order clause for the results.
string$pkField(optional) the field in the cross-reference table that maps to the primary key of the source object.
stringxrefField (optional) the field in the cross-refence table that maps to the primary key of the cross-referenced objects.

Definition at line 1286 of file data_item.inc.

1287  {
1288  $obj = new $class;
1289  $xref = new $xref;
1290 
1291  if ($orderBy != "")
1292  {
1293  foreach($obj->getFields() as $field => $type)
1294  {
1295  $orderBy = preg_replace("/\\b".$field."\\b/i", "a0.$field", $orderBy);
1296  }
1297  }
1298 
1299  $orderBy = preg_replace("/^\s*WHERE\s+/i", "AND ", $orderBy);
1300 
1301  $xt = $xref->table;
1302  $xp = ($xrefField != "") ? $xrefField : $obj->primary_key;
1303 
1304  $pkField = ($pkField != "") ? $pkField : $this->primary_key;
1305  $pk = $this->primary_key;
1306 
1307  $fieldList = $obj->getFieldList("a0");
1308 
1309  $query = "SELECT {$fieldList} FROM {$obj->table} a0, $xt x WHERE a0.{$obj->primary_key}=x.{$xp} AND x.{$pkField}={$this->$pk} $orderBy";
1310 
1311  trace($query, 3);
1312 
1313  try
1314  {
1315  $db = $this->getConnection();
1316 
1317  $result = $db->prepare($query);
1318  $result->execute();
1319 
1320  $list = array();
1321  while ($line = $result->fetch())
1322  {
1323  $obj = new $class;
1324  $obj->populate($line, "a0");
1325  $list[] = $obj;
1326  }
1327 
1328  unset($result);
1329  }
1330  catch(PDOException $e)
1331  {
1332  $err = "DataItem::crossReference() failed - " . $e->getMessage();
1333  trace($err, 2);
1334  throw new DataItemException($err);
1335  }
1336  return $list;
1337  }
getConnection()
Retrieves a connection to the database.

◆ DataItem()

DataItem::DataItem ( )

Constructor.

DataItems can be constructed in three ways:

  1. Empty objects can be constructed by calling the constructor with no arguments
  2. Objects can be instantiated from their primary key by passing in a single value
  3. Objects can be populated from an associative array of field/value pairs [such as those returned from mysql_fetch_assoc()]

Definition at line 97 of file data_item.inc.

98  {
99  if (func_num_args() > 0)
100  {
101  $arg = func_get_arg(0);
102 
103  if (is_array($arg) )
104  {
105  if (count($arg) > 1)
106  {
107  $this->populate($arg);
108  }
109  else if (count($arg) > 0)
110  {
111  if (is_array($arg[0]))
112  {
113  $this->populate($arg[0]);
114  }
115  else
116  {
117  $this->load($arg[0]);
118  }
119  }
120  }
121  else
122  {
123  if (func_num_args() > 1)
124  {
125  $this->filter = func_get_arg(1);
126  }
127 
128  $this->load($arg);
129  }
130  }
131  }
load($id)
Load the object with the specified primary key.
Definition: data_item.inc:561
populate($line, $alias=false)
Populates the object using the supplied associative array (field -> value).
Definition: data_item.inc:262

◆ delete()

DataItem::delete (   $constraint = "")

Delete the row in the database that corresponds to this object.

Reimplemented from AbstractDataItem.

Definition at line 888 of file data_item.inc.

889  {
890  if ($constraint == "")
891  {
892  $fire = true;
893  $pk = $this->primary_key;
894  $constraint = "WHERE $pk={$this->$pk} ".$this->getIdentityConstraint();
895  }
896 
897  $query = "DELETE FROM {$this->table} $constraint";
898 
899  trace($query, 3);
900 
901  $db = $this->getConnection();
902 
903  try
904  {
905  if ($fire) $this->fireEvent("onPreDelete");
906  $db->exec($query);
907  if ($fire) $this->fireEvent("onPostDelete");
908  }
909  catch(PDOException $e)
910  {
911  $err = "DataItem::delete() failed - " . $e->getMessage();
912  trace($err, 2);
913  throw new DataItemException($err);
914  }
915  }
fireEvent($event)
Fires the specified event to all registered handlers.
Definition: data_item.inc:169

◆ deleteAll()

DataItem::deleteAll ( )

Delete all the rows in the database that correspond to this class.

Reimplemented from AbstractDataItem.

Definition at line 920 of file data_item.inc.

921  {
922  //$query = "TRUNCATE TABLE {$this->table}";
923  $query = "DELETE FROM {$this->table}; ALTER TABLE {$this->table} AUTO_INCREMENT=1";
924  $db = $this->getConnection();
925 
926  trace($query, 3);
927 
928  try
929  {
930  $db->exec($query);
931  $this->fireEvent("onDeleteAll");
932  }
933  catch(PDOException $e)
934  {
935  $err = "DataItem::deleteAll() failed - " . $e->getMessage();
936  trace($err, 2);
937  throw new DataItemException($err);
938  }
939  }

◆ disablePrimaryKey()

DataItem::disablePrimaryKey ( )

Definition at line 229 of file data_item.inc.

230  {
231  if (!$this->_disablePK)
232  {
233  $conn = $this->getConnection();
234  $conn->prepare("ALTER TABLE `{$this->table}` DISABLE KEYS")->execute();
235  $this->_disablePK = true;
236  }
237  }

◆ distinctValues()

DataItem::distinctValues (   $field,
  $sorted = false,
  $constraint = "" 
)

Retrieves the distinct values in the database for the specified field across the specified set of records.

Parameters
string$field
boolean$sorted
string$constraint
Returns
array

Reimplemented from AbstractDataItem.

Definition at line 1001 of file data_item.inc.

1002  {
1003  $query = "SELECT DISTINCT $field from {$this->table} $constraint";
1004 
1005  trace($query, 3);
1006 
1007  $db = $this->getConnection();
1008 
1009  try
1010  {
1011  $result = $db->prepare($query);
1012  $result->execute();
1013 
1014  $values = array();
1015 
1016  while($line = $result->fetch())
1017  {
1018  $values[] = $line[$field];
1019  }
1020 
1021  if ($sorted)
1022  {
1023  sort($values);
1024  }
1025 
1026  unset($result);
1027 
1028  return $values;
1029  }
1030  catch(PDOException $e)
1031  {
1032  $err = "DataItem::distinctValues() failed - " . $e->getMessage();
1033  trace($err, 2);
1034  throw new DataItemException($err);
1035  }
1036 
1037  }

◆ duplicate()

DataItem::duplicate ( )

Persists a duplicate of the DataItem to the database under a new primary key.

Foreign keys are not updated. Use this as a way to create a quick copy of an existing object, or to import a serialized object into the data store under a new primary key. For utility, the previous value of the primary key is returned.

Returns
int the previous value of the primary key

Definition at line 983 of file data_item.inc.

984  {
985  $pk = $this->primary_key;
986  $old = $this->get($pk);
987 
988  unset($this->$pk);
989  $this->save();
990  return $old;
991  }
save()
Store the object in the database.
Definition: data_item.inc:636

◆ enablePrimaryKey()

DataItem::enablePrimaryKey ( )

Definition at line 239 of file data_item.inc.

240  {
241  if ($this->_disablePK)
242  {
243  $conn = $this->getConnection();
244  $conn->prepare("ALTER TABLE `{$this->table}` ENABLE KEYS")->execute();
245  $this->_disablePK = false;
246  }
247  }

◆ exists()

DataItem::exists (   $constraint = "")

Check whether the object exists in the database.

Reimplemented from AbstractDataItem.

Definition at line 665 of file data_item.inc.

666  {
667 
668  $pk = $this->primary_key;
669 
670  if (!$constraint)
671  {
672  if ($this->$pk == "" || $this->$pk == 0) return false;
673  $constraint = "WHERE $pk={$this->$pk}";
674  }
675 
676  $query = "SELECT $pk FROM {$this->table} $constraint";
677  trace("DataItem::exists() - $query", 3);
678 
679  $db = $this->getConnection();
680  $exists = false;
681 
682  try
683  {
684  $result = $db->prepare($query);
685  $result->execute();
686 
687  if ($line = $result->fetch())
688  {
689  $exists = true;
690  }
691 
692  unset($result);
693  }
694  catch(PDOException $e)
695  {
696  $err = "DataItem::exists() failed - " . $e->getMessage();
697  trace($err, 2);
698  throw new DataItemException($err);
699  }
700 
701  return $exists;
702  }

◆ fireEvent()

DataItem::fireEvent (   $event)

Fires the specified event to all registered handlers.

The firing object is passed as a parameter to all the registered handlers.

Parameters
string$eventthe event to be fired

Definition at line 169 of file data_item.inc.

170  {
171  $eventKey = get_class($this)."_{$event}";
172  if (!array_key_exists($eventKey, DataItem::$_eventMap))
173  {
174  return;
175  }
176 
177  trace("===== DataItem event key = $eventKey", 3);
178  $handlers = DataItem::$_eventMap[$eventKey];
179 
180  if (!is_array($handlers)) return;
181  foreach($handlers as $handler)
182  {
183  if (!is_callable($handler))
184  {
185  trace(get_class($this)." {$event} handler is invalid", 2);
186  }
187  else
188  {
189  call_user_func_array($handler, array($this));
190  }
191  }
192  }
static $_eventMap
Definition: data_item.inc:63

◆ format()

DataItem::format (   $template = "",
  $separator = ", " 
)

Substitute values for field names in a string, with the fields formatted using their type's default format or according to the formatting template sting passed in.

example template string: "{program_name} {start_date} {number_students}"

  1. data type format specifications: The start_date field can have formatting specifications such as "{start_date:long} for long format. For more information on formatting specifications following the colon, check comments in DataTypeRenderer classes.
  2. Default value specification: syntax: {field|alternate} example: {name|N/A}

In the above example "N/A" would be output if the name field is empty or null. This will also work with numeric fields with a value of 0 and booleans with false. Very useful for places where you are linking to a form by name or title.

  1. Inline static method call: syntax: {class::method} example: {MyHelper::getComplicatedOutput} This works the same as passing array(MyHelper, getComplicatedOutput) to a DataListView. It calls the static method getComplicatedOutput() on the MyHelper class, passing the current object in as a parameter. The new inline format means that you can use it as part of more complicated expressions like: "{name} - {MyHelper::getComplicatedOutput}"
  2. Retrieve field value through relations.

Fields can also be retrieved through a relation from the dataitem obj that calls this format function.

  1. Local method calls

Syntax: {method()} Example: {getFullName()}

This format calls the specified method on the object and substitutes the returned value into the output. Parameters cannot be passed.

for example: "{MyRelation.event_name}"

Variables used: $format - stores the pattern inside the braces {} for search & replace substitution in the template string.

$fieldFormat - the requested formatting for the field, if any.

$sub - the value to be substituted or replacement value in the tempalte string.

Reimplemented from AbstractDataItem.

Definition at line 1582 of file data_item.inc.

1583  {
1584  if ($template == "") $template = $this->default_format;
1585  if (is_array($template) && is_callable($template))
1586  {
1587  return call_user_func($template, $this);
1588  }
1589 
1590  foreach($this->getFields() as $field => $type)
1591  {
1592  $template = str_replace("{".$field."}", $this->formatFieldValue($field), $template);
1593  }
1594 
1595  if (isset($this->calculatedFields))
1596  {
1597  foreach($this->calculatedFields as $field => $expr)
1598  {
1599  $template = str_replace("{".$field."}", $this->$field, $template);
1600  }
1601  }
1602 
1603  if (isset($this->_decorations))
1604  {
1605  foreach($this->_decorations as $field => $value)
1606  {
1607  $template = str_replace("{".$field."}", $value, $template);
1608  }
1609  }
1610 
1611  // Allow drill-down through relation functions
1612  $template = $this->formatThroughRelation($template, $separator);
1613 
1614  $template = $this->formatField($template);
1615 
1616  // Simple method calls - no parameter passing
1617  // ??Is this block ever executed??
1618 
1619  $matches = array();
1620  preg_match_all("/\\{([\\w_]+)\\(\\)\\}/", $template, $matches, PREG_SET_ORDER);
1621 
1622  foreach($matches as $match)
1623  {
1624  $format = $match[0];
1625  $method = $match[1];
1626 
1627  $value = $this->$method();
1628 
1629  $template = str_replace($format, $value, $template);
1630  }
1631 
1632  return $template;
1633  }
formatField($template)
Definition: data_item.inc:1643
formatThroughRelation($template, $separator=", ")
Definition: data_item.inc:1713

◆ formatField()

DataItem::formatField (   $template)

Definition at line 1643 of file data_item.inc.

1644  {
1645  $matches = array();
1646 
1647  preg_match_all("/\\{([\\w\\d_]+)(::?|\\^|\\(\\)|\\|)?([^}]*)}/", $template, $matches, PREG_SET_ORDER);
1648 
1649  foreach($matches as $match)
1650  {
1651  $field = $match[1];
1652  // JDG 3/12 fix issue with call to function in DataItem in format like "{getTitle()}" with no separator;
1653  $separator = ($match[2]) ? $match[2] : ":";
1654 
1655  $fieldFormat = $match[3];
1656  trace("formatField, template $template, field $field, separator $separator and fieldFormat $fieldFormat", 5);
1657 
1658  $format = "{".$field.$separator.$fieldFormat."}";
1659 
1660  switch($separator)
1661  {
1662  case "|":
1663  if (!$this->get($field))
1664  {
1665  $sub = $fieldFormat;
1666  }
1667  else
1668  {
1669  $sub = $this->formatFieldValue($field);
1670  }
1671  break;
1672 
1673  case "^":
1674  if (!$this->get($field))
1675  {
1676  $sub = $this->format("{".$fieldFormat."}");
1677  }
1678  else
1679  {
1680  $sub = $this->formatFieldValue($field);
1681  }
1682  break;
1683 
1684  case "::":
1685 
1686  $sub = call_user_func(array($field, $fieldFormat), $this);
1687  break;
1688 
1689  case "()":
1690 
1691  $sub = $this->$field();
1692  break;
1693 
1694  case ":":
1695  default:
1696  $sub = $this->formatFieldValue($field, $fieldFormat);
1697  }
1698 
1699  $template = str_replace($format, $sub, $template);
1700  }
1701  return $template;
1702  }
formatFieldValue($field, $template="")
format($template="", $separator=", ")
Substitute values for field names in a string, with the fields formatted using their type's default f...
Definition: data_item.inc:1582

◆ formatThroughRelation()

DataItem::formatThroughRelation (   $template,
  $separator = ", " 
)

Definition at line 1713 of file data_item.inc.

1714  {
1715  $matches = array();
1716  preg_match_all("/\\{([\\w\\d_]+)\\.([\\w\\d_\\.\\(\\)]+)([:\\|\\^])?([^}]*)}/", $template, $matches, PREG_SET_ORDER);
1717 
1718  foreach($matches as $match)
1719  {
1720  $relation = $match[1];
1721  $field = $match[2];
1722  $divider = $match[3];
1723  if(count($match) == 5 && preg_match("/^where|order/i", $match[4]))
1724  $constraint = $match[4];
1725  elseif(count($match) == 5)
1726  $fieldTemplate = $match[4];
1727 
1728  trace("function format: formatThroughRelations template $template field $field relation $relation constraint $constraint", 5);
1729 
1730  if ($constraint)
1731  {
1732  $relations = $this->$relation($constraint);
1733  $format = "{".$relation.".".$field.$divider.$constraint."}";
1734  }
1735  elseif($fieldTemplate)
1736  {
1737  $relations = $this->$relation();
1738  $format = "{".$relation.".".$field.$divider.$fieldTemplate."}";
1739  }
1740  else
1741  {
1742  $relations = $this->$relation();
1743  $format = "{".$relation.".".$field."}";
1744  }
1745 
1746  $sub = "";
1747 
1748  if (is_array($relations))
1749  {
1750  $formatTemplate = ($fieldTemplate) ? "{".$field .":".$fieldTemplate . "}" : "{".$field."}";
1751  $sub = formatItems($relations, $formatTemplate, $separator);
1752 
1753  }
1754  else if ($relations)
1755  {
1756  if ($fieldTemplate) $fieldTemplate = ":{$fieldTemplate}";
1757  if ($relations) $sub = $relations->format("{{$field}{$fieldTemplate}}");
1758  }
1759 
1760  trace("format using relations: Replacing {$match[0]} [ $format ] with '$sub'", 5);
1761 
1762  $template = str_replace($format, $sub, $template);
1763  }
1764 
1765  return $template;
1766  }
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
Definition: data_item.inc:2176

◆ fromDataSet()

DataItem::fromDataSet (   $params)

Automatically populate the object based on a custom set of parameters (such as a filtered $_POST collection)

Definition at line 1102 of file data_item.inc.

1103  {
1104  foreach($this->getFields() as $field => $type)
1105  {
1106  if ($this->filter && $this->filter->isExcluded($field)) continue;
1107  if (array_key_exists($field, $params))
1108  {
1109  $this->$field = $params[$field];
1110  }
1111  elseif (!array_key_exists($field, $params) AND ($type == Boolean))
1112  {
1113  $this->$field = 0;
1114  }
1115  }
1116 
1117  $this->unpack();
1118  }
unpack()
The unpack() method is called after populating a DataItem.
Definition: data_item.inc:552

◆ fromGET()

DataItem::fromGET ( )

Automatically populate the object based on parameters in the $_GET collection.

Reimplemented from AbstractDataItem.

Definition at line 1042 of file data_item.inc.

1043  {
1044  foreach($this->getFields() as $field => $type)
1045  {
1046  if ($this->filter && $this->filter->isExcluded($field)) continue;
1047  if (array_key_exists($field, $_GET))
1048  {
1049  $this->set($field, $_GET[$field]);
1050  }
1051  }
1052 
1053  $this->unpack();
1054  }

◆ fromPOST()

DataItem::fromPOST ( )

Automatically populate the object based on parameters in the $_POST collection.

If the key does not exist in $_POST and the type is Boolean, that means that the user unchecked a checkbox and we need to assign 0 to the field.

Reimplemented from AbstractDataItem.

Definition at line 1062 of file data_item.inc.

1063  {
1064  foreach($this->getFields() as $field => $type)
1065  {
1066  if ($this->filter && $this->filter->isExcluded($field)) continue;
1067  if (array_key_exists($field, $_POST))
1068  {
1069  $this->set($field, $_POST[$field]);
1070  }
1071  elseif (!array_key_exists($field, $_POST) AND ($type == Boolean))
1072  {
1073  $this->$field = 0;
1074  }
1075  }
1076 
1077  $this->unpack();
1078  }

◆ fromREQUEST()

DataItem::fromREQUEST ( )

Automatically populate the object based on parameters in either the $_GET or $_POST collection, depending on the method used to access the page.

Reimplemented from AbstractDataItem.

Definition at line 1084 of file data_item.inc.

1085  {
1086  switch($_SERVER["REQUEST_METHOD"])
1087  {
1088  case "POST":
1089  $this->fromPOST();
1090  break;
1091 
1092  case "GET":
1093  default:
1094  $this->fromGET();
1095  }
1096  }
fromPOST()
Automatically populate the object based on parameters in the $_POST collection.
Definition: data_item.inc:1062
fromGET()
Automatically populate the object based on parameters in the $_GET collection.
Definition: data_item.inc:1042

◆ fromXML()

DataItem::fromXML (   $node)

Populates the object from the specified XML node.

Parameters
DOMNode$node

Reimplemented from AbstractDataItem.

Definition at line 1485 of file data_item.inc.

1486  {
1487  if ($node->nodeType != XML_ELEMENT_NODE || $node->tagName != get_class($this)) return;
1488 
1489  $kids = $node->childNodes;
1490  $numChildren = $kids->length;
1491 
1492  for($i = 0; $i < $numChildren; ++$i)
1493  {
1494  $n = $kids->item($i);
1495 
1496  if ($n->nodeType != XML_ELEMENT_NODE) continue;
1497  $field = $n->tagName;
1498  $this->set($field, $n->nodeValue);
1499  }
1500  }

◆ get()

DataItem::get (   $field)

Retrieve the value for the specified field.

Parameters
string$fieldthe field to retrieve
Returns
mixed the value of that field

Reimplemented from AbstractDataItem.

Definition at line 474 of file data_item.inc.

475  {
476  if (!$field) throw new FakoliException("Cannot access empty field");
477  return $this->$field;
478  }

◆ getFieldAliases()

DataItem::getFieldAliases ( )

Retrieve the list of field aliases.

Returns
array the alias list for this object

Reimplemented from AbstractDataItem.

Definition at line 523 of file data_item.inc.

524  {
525  return $this->fieldAliases;
526  }

◆ getFieldAnnotations()

DataItem::getFieldAnnotations ( )

Retrieve the list of field annotations.

Returns
array the annotation list for this object

Reimplemented from AbstractDataItem.

Definition at line 532 of file data_item.inc.

533  {
534  return $this->fieldAnnotations;
535  }

◆ getFieldArray()

DataItem::getFieldArray ( )

Return an array of field names for this object filtered by any active filter.

Returns
array

Reimplemented from AbstractDataItem.

Definition at line 415 of file data_item.inc.

416  {
417  $arr = array();
418  foreach($this->getFields() as $field => $type)
419  {
420  if ($this->filter && $this->filter->isExcluded($field)) continue;
421  $arr[] = $field;
422  }
423 
424  return $arr;
425  }

◆ getFieldList()

DataItem::getFieldList (   $alias = "")

Returned a comma-separated list of the fields for this object (applying the assigned filter if there is one).

Reimplemented from AbstractDataItem.

Definition at line 361 of file data_item.inc.

362  {
363  $list = "";
364  $first = true;
365 
366  foreach($this->getFields() as $field => $type)
367  {
368  if ($this->filter && $this->filter->isExcluded($field)) continue;
369  if (!$first) $list .= ", ";
370  if ($alias)
371  {
372  $list .= "$alias.";
373  }
374  $list .= "`".$field."`";
375  if ($alias)
376  {
377  $list .= " as `$alias.$field`";
378  }
379  $first = false;
380  }
381 
382  if (isset($this->calculatedFields))
383  {
384  foreach($this->calculatedFields as $field => $expr)
385  {
386  if ($this->filter && $this->filter->isExcluded($field)) continue;
387  if (!$first) $list .= ", ";
388 
389  if ($alias)
390  {
391  foreach($this->getFields() as $f => $type)
392  {
393  $expr = preg_replace('/([\s\‍(])('.$f.')\b/', "$1{$alias}.$2", $expr);
394  $expr = preg_replace('/\b'.$this->table.'\.'.$f.'\b/', "{$alias}.{$f}", $expr);
395  }
396  }
397 
398  $list .= $expr . " as `";
399  if ($alias)
400  {
401  $list .= $alias.".";
402  }
403  $list .= $field."`";
404 
405  }
406  }
407 
408  return $list;
409  }

◆ getFields()

DataItem::getFields ( )

Retrieve the field type list for this object.

Reimplemented from AbstractDataItem.

Definition at line 333 of file data_item.inc.

334  {
335  if ($this->fields) return $this->fields;
336  $class = get_class($this);
337  if ($class::$fields) return $class::$fields;
338 
339  throw new FakoliException("DataItem does not contain any fields");
340  }

◆ getFilter()

DataItem::getFilter ( )

Returns the filter set on this object.

Reimplemented from AbstractDataItem.

Definition at line 505 of file data_item.inc.

506  {
507  return $this->filter;
508  }

◆ getHiddenFields()

DataItem::getHiddenFields ( )

Retrieves the list of hidden fields.

These will be automatically hidden in any AutoForm.

Returns
array of names of hidden fields.

Reimplemented from AbstractDataItem.

Definition at line 249 of file data_item.inc.

250  {
251  return $this->hiddenFields;
252  }

◆ getPrimaryKey()

DataItem::getPrimaryKey ( )

Retrieves the primary key field name.

Reimplemented from AbstractDataItem.

Definition at line 455 of file data_item.inc.

456  {
457  return $this->primary_key;
458  }

◆ getPrimaryKeyList()

DataItem::getPrimaryKeyList ( )

Retrieves a list of all the primary keys used for an object as an array.

Returns
array all the primary keys for this object's constituent parts

Reimplemented from AbstractDataItem.

Definition at line 464 of file data_item.inc.

465  {
466  return array($this->primary_key);
467  }

◆ getRelated()

DataItem::getRelated (   $class,
  $field = "" 
)

Returns a single item related by the specified foreign key.

The foreign key name must match the primary key name for the specified class.

Use this method to implement one-to-one relations.

Parameters
string$classthe name of the related class
string$fieldthe name of the foreign key defining the relation.

Definition at line 1210 of file data_item.inc.

1211  {
1212  $obj = new $class;
1213  if ($field == "")
1214  {
1215  $field = $obj->primary_key;
1216 
1217  }
1218 
1219  if (array_key_exists($field, $this->getFields()))
1220  {
1221  $pk = $obj->primary_key;
1222  $val = $this->get($field);
1223 
1224  if (!$val) return null;
1225 
1226  $obj->$pk = $val;
1227 
1228  $cache = "_{$class}_{$field}_{$val}";
1229  if (isset($this->$cache))
1230  {
1231  $obj->copy($this->$cache);
1232  }
1233  else
1234  {
1235  $obj->select();
1236  $this->$cache = $obj;
1237  }
1238  }
1239  else
1240  {
1241  $field = $this->primary_key;
1242  $obj = querySingle($class, "WHERE $field={$this->$field}");
1243  }
1244  return $obj;
1245  }
querySingle($class)
Performs a query against the database and returns a matching singleton object.
Definition: data_item.inc:1810

◆ getRelatedList()

DataItem::getRelatedList (   $class,
  $field = "",
  $orderBy = "" 
)

Returns a list of items that are related to this item.

Usually the items will be related by having a foreign key field that matches the primary key on the calling object. However, another field on the calling object can be specified if required. You can also optionally specify a sort order for the results.

Use this field to implement one-to-many relationships.

Parameters
string$classthe name of the related class
string$field(optional) the name of the field defining the relation
string$orderBy(optional) a sort order clause for the results

Definition at line 1260 of file data_item.inc.

1261  {
1262  $pk = $this->primary_key;
1263 
1264  if ($field == "") $field = $pk;
1265  $value = $this->$field ? $this->$field : $this->$pk;
1266  $orderBy = preg_replace("/^\s*WHERE\s+/i", "AND ", $orderBy);
1267  //$this->quoteFieldValue($this->$field, $this->fields[$field])
1268  $list = query($class, "WHERE $field=$value $orderBy");
1269 
1270  return $list;
1271  }
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
Definition: query.inc:373

◆ getTransaction()

DataItem::getTransaction ( )

Retrieves the current DataTransaction.

Reimplemented from AbstractDataItem.

Definition at line 206 of file data_item.inc.

207  {
208  return $this->_tx;
209  }

◆ getType()

DataItem::getType (   $field)

Retrieves the data type of the specified field.

Parameters
$fieldthe field in question
Returns
string the data type of the specified field

Reimplemented from AbstractDataItem.

Definition at line 496 of file data_item.inc.

497  {
498  $fields = $this->getFields();
499  return $fields[$field];
500  }

◆ hasField()

DataItem::hasField (   $field)

Returns true if this DataItem contains a field with the specified name and that field is not excluded by a filter.

Parameters
string$field
Returns
true if this field is present and not filtered, false otherwise.

Reimplemented from AbstractDataItem.

Definition at line 434 of file data_item.inc.

435  {
436  if (!array_key_exists($field, $this->getFields())) return false;
437  if ($this->filter && $this->filter->isExcluded($field)) return false;
438  return true;
439  }

◆ hasRelation()

DataItem::hasRelation (   $relation)

Returns true if this DataItem contains a relation with the specified name.

Parameters
string$relation
Returns
true if this relation is present and not filtered, false otherwise.

Reimplemented from AbstractDataItem.

Definition at line 447 of file data_item.inc.

448  {
449  return (!array_key_exists($relation, $this->relations)) ? false : true;
450  }

◆ insert()

DataItem::insert ( )

Insert a new row in the database to store this object.

Reimplemented from AbstractDataItem.

Definition at line 812 of file data_item.inc.

813  {
814  $this->pack();
815 
816  $this->fireEvent("beforeInsert");
817 
818  $pk = $this->primary_key;
819 
820  $first = true;
821 
822  $timestampField = null;
823 
824  foreach($this->getFields() as $field => $type)
825  {
826  if ($field == $pk && !$this->_disablePK) continue;
827  if ($this->filter && $this->filter->isExcluded($field)) continue;
828  if (!isset($this->$field) && $this->getType($field) != Timestamp && $this->getType($field) != Boolean) continue;
829 
830  if ($type == Timestamp)
831  {
832  $timestampField = $field;
833  }
834 
835  if (!$first)
836  {
837  $fields .= ", ";
838  $values .= ", ";
839  }
840 
841  $fields .= "`".$field."`";
842  $values .= $this->quoteFieldValue($field, $type);
843 
844  $first = false;
845  }
846 
847  $query = "INSERT INTO {$this->table} ($fields) values ($values)";
848 
849  trace("DataItem::insert() - $query", 3);
850 
851  $success = false;
852 
853  try
854  {
855  $db = $this->getConnection();
856  $success = $db->exec($query);
857 
858  if ($success !== FALSE)
859  {
860  $this->$pk = $db->lastInsertId();
861  //AJG - Set the timestamp field value if the insert succeeded
862  if ($timestampField)
863  {
864  $this->$timestampField = $this->__timestamp;
865  unset($this->__timestamp);
866  }
867 
868  $this->fireEvent("onInsert");
869  }
870  else
871  {
872  $this->$pk = 0;
873  trace("DataItem::insert() failed - $query", 2);
874  }
875  }
876  catch(PDOException $e)
877  {
878  $err = "DataItem::insert() failed - ". $e->getMessage();
879  trace($err, 2);
880  throw new DataItemException($err);
881  }
882  return $success;
883  }
quoteFieldValue($field, $type=null)
Returns the properly quoted value of the specified field.
getType($field)
Retrieves the data type of the specified field.
Definition: data_item.inc:496
pack()
The pack() method is called prior to persisting a DataItem to storage.
Definition: data_item.inc:543

◆ joinTransaction()

DataItem::joinTransaction (   $tx)

Join the DataItem to the specified DataTransaction.

Parameters
DataTransaction$txthe transaction to join

Reimplemented from AbstractDataItem.

Definition at line 198 of file data_item.inc.

199  {
200  $this->_tx = $tx;
201  }

◆ load()

DataItem::load (   $id)

Load the object with the specified primary key.

Parameters
intid the primary key value to instantiate from

Reimplemented from AbstractDataItem.

Definition at line 561 of file data_item.inc.

562  {
563  if ($this->cacheLookup($id)) return;
564 
565  $fields = $this->getFieldList();
566  $query = "SELECT $fields FROM {$this->table} WHERE {$this->primary_key}=$id ".$this->getIdentityConstraint();
567  trace("DataItem::load($id): $query", 3);
568 
569  $db = $this->getConnection();
570 
571  try
572  {
573  $result = $db->prepare($query);
574  $result->execute();
575 
576  if ($line = $result->fetch())
577  {
578  $this->populate($line);
579  }
580 
581  unset($result);
582  }
583  catch(PDOException $e)
584  {
585  $err = "DataItem::load() failed - ".$e->getMessage();
586  trace($err, 2);
587  throw new DataItemException($err);
588  }
589  }
cacheLookup($id)
Populate the object from the local cache if the object is marked as "cacheLocal".
Definition: data_item.inc:1179
getFieldList($alias="")
Returned a comma-separated list of the fields for this object (applying the assigned filter if there ...
Definition: data_item.inc:361

◆ loadComposite()

DataItem::loadComposite ( )

Creates an outer CompositeDataItem from its base component.

In order for this method to succeed, the base component table must have a 'composite_class' field that is populated with the class name of the correct CompositeDataItem class.

Returns
CompositeDataItem the containing CompositeDataItem for this base component.

Definition at line 620 of file data_item.inc.

621  {
622  if (!$this->hasField("composite_class") || !$this->composite_class)
623  {
624  throw new DataItemException("Cannot create composite from ".get_class($this)." as required composite cannot be determined");
625  }
626 
627  $composite = new $this->composite_class;
628  $composite->loadFromBase($this->get($this->getPrimaryKey()));
629 
630  return $composite;
631  }
hasField($field)
Returns true if this DataItem contains a field with the specified name and that field is not excluded...
Definition: data_item.inc:434
getPrimaryKey()
Retrieves the primary key field name.
Definition: data_item.inc:455

◆ old()

DataItem::old ( )

Retrieves the currently stored state of the DataItem from the database and returns it as a new object.

This can be used for comparisons when updating a DataItem, for instance to trigger related updates when a field value is changed.

Returns
DataItem a DataItem populated from the database based on the current item's primary key. The returned DataItem has the same filter and DataTransaction as the parent item.

Definition at line 601 of file data_item.inc.

602  {
603  $pk = $this->getPrimaryKeyValue();
604  if (!$pk) return null;
605 
606  $cl = get_class($this);
607  $n = new $cl;
608  $n->filter = $this->filter;
609  $n->joinTransaction($this->getTransaction());
610  $n->load($pk);
611  return $n;
612  }
getPrimaryKeyValue()
Retrieves the primary key value.
getTransaction()
Retrieves the current DataTransaction.
Definition: data_item.inc:206

◆ overrideFieldType()

DataItem::overrideFieldType (   $field,
  $type 
)

Override the type for the specified field.

Parameters
string$field
string$type

Definition at line 347 of file data_item.inc.

348  {
349  if (!is_array($this->fields))
350  {
351  throw new FakoliException("Cannot override field types for statically defined DataItems");
352  }
353 
354  $this->fields[$field] = $type;
355  }

◆ pack()

DataItem::pack ( )

The pack() method is called prior to persisting a DataItem to storage.

It can be used to perform a preprocessing step prior to storage, such as formatting sub-objects into a JSON, XML or other serialized format and storing them in a field.

Definition at line 543 of file data_item.inc.

544  {
545  }

◆ populate()

DataItem::populate (   $line,
  $alias = false 
)

Populates the object using the supplied associative array (field -> value).

Only values that match the definition of the sub-classed object will be copied. Fields that have been excluded by the use of an InclusionFilter or ExclusionFilter will also not be populated.

Parameters
array$linethe array of values to populate the object with.

Definition at line 262 of file data_item.inc.

263  {
264  global $config;
265 
266  // NOTE - structured for speed, since the general case is no aliasing
267  // therefore we want to reduce the number of tests of the alias
268 
269  $fields = $this->getFields();
270 
271  if ($alias)
272  {
273  $offset = strlen($alias) + 1;
274 
275  foreach ($line as $field => $value)
276  {
277  if (!startsWith($field, $alias)) continue;
278  $field = substr($field, $offset);
279 
280  if (!array_key_exists($field, $fields)) continue;
281  if ($this->filter && $this->filter->isExcluded($field)) continue;
282 
283  if (!isset($config["no_charset_conversions"]) || $config["no_charset_conversions"])
284  {
285  $this->$field = $value;
286  }
287  else
288  {
289  $this->$field = iconv("UTF-8", "CP1252//IGNORE", $value);
290  }
291  }
292  }
293  else
294  {
295  foreach ($line as $field => $value)
296  {
297  if (!array_key_exists($field, $fields)) continue;
298  if ($this->filter && $this->filter->isExcluded($field)) continue;
299 
300  if (!isset($config["no_charset_conversions"]) || $config["no_charset_conversions"])
301  {
302  $this->$field = $value;
303  }
304  else
305  {
306  $this->$field = iconv("UTF-8", "CP1252//IGNORE", $value);
307  }
308  }
309  }
310 
311  if (isset($this->calculatedFields))
312  {
313  foreach($this->calculatedFields as $field => $expr)
314  {
315  if ($alias)
316  {
317  $this->$field = $line[$alias.".".$field];
318  }
319  else
320  {
321  $this->$field = $line[$field];
322  }
323  }
324  }
325 
326  $this->unpack();
327  $this->fireEvent("onPopulate");
328  }
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
Definition: functions.inc:1470

◆ prettifyFieldName()

DataItem::prettifyFieldName (   $field)

Reimplemented from AbstractDataItem.

Definition at line 1769 of file data_item.inc.

1770  {
1771  if (isset($this->fieldAliases) && array_key_exists($field, $this->fieldAliases))
1772  {
1773  return $this->fieldAliases[$field];
1774  }
1775 
1776  $field = preg_replace("/([a-z])([A-Z0-9])/", "$1 $2", $field);
1777  $field = str_replace("_", " ", $field);
1778  $field = ucwords($field);
1779 
1780  return $field;
1781  }

◆ queryValue()

DataItem::queryValue (   $func)

Query the database to calculate an aggregate value.

The database table associated with the instatiated object is used as the source for the data.

Parameters
string$functhe value or function to retrieve
string$constraintsoptional constraint clause to apply to the query

Definition at line 1346 of file data_item.inc.

1347  {
1348  $constraints = "";
1349  $value = null;
1350 
1351  if (func_num_args() > 1)
1352  {
1353  $constraints = func_get_arg(1);
1354 
1355  if (func_num_args() > 2)
1356  {
1357  $value = func_get_arg(2);
1358  }
1359  }
1360 
1361  if ($constraints == "") $constraints = "WHERE 1=1"; //TODO - tidy this up some day
1362  $constraints .= " ".$this->getIdentityConstraint();
1363 
1364  $query = "SELECT $func as result FROM {$this->table} $constraints";
1365 
1366  trace("DataItem::queryValue: $query", 3);
1367  try
1368  {
1369  $db = $this->getConnection();
1370  $result = $db->prepare($query);
1371  $result->execute();
1372 
1373  if ($row =$result->fetch())
1374  {
1375  $value = $row['result'];
1376  }
1377 
1378  unset($result);
1379  }
1380  catch(PDOException $e)
1381  {
1382  $err = "DataItem::queryValue() failed - " . $e->getMessage();
1383  trace($err, 2);
1384  throw new DataItemException($err);
1385  }
1386 
1387  return $value;
1388 
1389  }

◆ registerEventHandler()

static DataItem::registerEventHandler (   $class,
  $event,
  $handler 
)
static

Register an event handler for a specific event and DataItem class.

DataItem events are fired by DataItems at various points in the data lifecycle (such as inserting, updating, deleting, etc.)

Parameters
string$classthe DataItem class to which the event handler is to be attached
string$eventthe name of the event
callable$handlerthe event handler (a callable function)

Definition at line 151 of file data_item.inc.

152  {
153  $eventKey = "{$class}_{$event}";
154 
155  if (!array_key_exists($eventKey, DataItem::$_eventMap))
156  {
157 
158  DataItem::$_eventMap[$eventKey]= array();
159  }
160 
161  array_push(DataItem::$_eventMap[$eventKey], $handler);
162  }

◆ relateTo()

DataItem::relateTo (   $target,
  $field = "" 
)

Link this object to the specified target by setting corresponding field to the value of the target's primary key.

Parameters
DataItem$targetthe DataItem to which the relationship will be created
string$fieldthe field to set for the relationship. If not specified, the primary key of the target object is used for the foreign key field name.

Reimplemented from AbstractDataItem.

Definition at line 1791 of file data_item.inc.

1792  {
1793  $pk = $target->primary_key;
1794 
1795  if (!$field) $field = $pk;
1796 
1797  $this->$field = $target->$pk;
1798  }

◆ save()

DataItem::save ( )

Store the object in the database.

Reimplemented from AbstractDataItem.

Definition at line 636 of file data_item.inc.

637  {
638  if ($this->cacheLocal)
639  {
640  Cache::invalidate(get_class($this)."_cache");
641  }
642 
643  if ($this->exists())
644  {
645  return $this->update();
646  }
647  else
648  {
649  return $this->insert();
650  }
651  }
static invalidate($key)
Invalidates the specifed entry in the cache.
Definition: cache.inc:119
exists($constraint="")
Check whether the object exists in the database.
Definition: data_item.inc:665
insert()
Insert a new row in the database to store this object.
Definition: data_item.inc:812
update()
Update the row in the database that corresponds to this object.
Definition: data_item.inc:707

◆ select()

DataItem::select ( )

Select the object from the database, based on the value of the primary key field.

Reimplemented from AbstractDataItem.

Definition at line 656 of file data_item.inc.

657  {
658  $pk = $this->primary_key;
659  $this->load($this->$pk);
660  }

◆ set()

DataItem::set (   $field,
  $value 
)

Set the value of the specified field.

Parameters
$fieldthe field to set
$valuethe value to which the field is to be set

Reimplemented from AbstractDataItem.

Definition at line 485 of file data_item.inc.

486  {
487  if (!$field) throw new FakoliException("Cannot access empty field");
488  $this->$field = $value;
489  }

◆ setFilter()

DataItem::setFilter (   $filter)

Sets the filter on this object.

Parameters
$filterthe filter

Reimplemented from AbstractDataItem.

Definition at line 514 of file data_item.inc.

515  {
516  $this->filter = $filter;
517  }

◆ tableExists()

DataItem::tableExists ( )

Check if the table for this class exists in the database.

Definition at line 944 of file data_item.inc.

945  {
946  $query = "SHOW TABLES LIKE '{$this->table}'";
947  $db = $this->getConnection();
948 
949  trace($query, 3);
950 
951  $exists = false;
952 
953  try
954  {
955  $result = $db->prepare($query);
956  $result->execute();
957 
958  if ($line = $result->fetch())
959  {
960  $exists = true;
961  }
962 
963  unset($result);
964  }
965  catch(PDOException $e)
966  {
967  $err = "DataItem::tableExists() failed - " . $e->getMessage();
968  trace($err, 2);
969  throw new DataItemException($err);
970  }
971 
972  return $exists;
973  }

◆ toJSON()

DataItem::toJSON ( )

Definition at line 1502 of file data_item.inc.

1503  {
1504  $out = array();
1505 
1506  foreach($this->getFields() as $field => $type)
1507  {
1508  if ($this->filter && $this->filter->isExcluded($field))
1509  {
1510  continue;
1511  }
1512 
1513  $val = str_replace("\\'", "'", json_encode((string)$this->get($field)));
1514 
1515  $out[] = "\"{$field}\": ".$val;
1516  }
1517 
1518  return "{".implode(", ", $out)."}";
1519  }

◆ toXML()

DataItem::toXML (   $indent = 0,
  $path = null 
)

Generates an XML representation of the object.

Filters are honored when determining which fields are included in the XML. Empty fields are not output (note that a string field with value "" is not considered empty).

Reimplemented from AbstractDataItem.

Definition at line 1396 of file data_item.inc.

1397  {
1398  trace(get_class($this)."->toXML()", 4);
1399 
1400  $xml = str_repeat(" ", $indent) . "<" . get_class($this) . ">\n";
1401 
1402  foreach($this->getFields() as $field => $type)
1403  {
1404  if ($this->filter && $this->filter->isExcluded($field)) continue;
1405  if (isset($this->$field))
1406  {
1407  if ($this->$field === "")
1408  {
1409  $xml .= str_repeat(" ", $indent) . " <$field/>\n";
1410  }
1411  else
1412  {
1413  $xml .= str_repeat(" ", $indent) . " <{$field}>".$this->formatFieldForXML($field)."</$field>\n";
1414  }
1415 
1416  }
1417  }
1418 
1419  $path[get_class($this)] = true;
1420  trace(implode(" > ", array_keys($path)), 4);
1421  trace("Options: ".$this->_options, 4);
1422 
1423  if (($this->_options & SerializeRelations) ||
1424  ($this->_options & SerializeDirectRelations) &&
1425  $this->relations)
1426  {
1427  if (is_array($this->relations))
1428  {
1429  foreach($this->relations as $rel => $relType)
1430  {
1431  if (is_array($path) && array_key_exists($relType, $path)) continue;
1432 
1433  $obj = $this->$rel();
1434  if ($obj)
1435  {
1436  $xml .= str_repeat(" ", $indent) . " <$rel>\n";
1437 
1438  if (is_array($obj))
1439  {
1440  foreach($obj as $item)
1441  {
1442  if ($this->_options & SerializeRelations)
1443  {
1444  $item->setOption(SerializeRelations);
1445  }
1446 
1447  if ($this->_options & ProtectHTML)
1448  {
1449  $item->setOption(ProtectHTML);
1450  }
1451 
1452  $xml .= $item->toXML($indent + 2, $path);
1453  }
1454  }
1455  else
1456  {
1457  if ($this->_options & SerializeRelations)
1458  {
1459  $obj->setOption(SerializeRelations);
1460  }
1461 
1462  if ($this->_options & ProtectHTML)
1463  {
1464  $obj->setOption(ProtectHTML);
1465  }
1466 
1467  $xml .= $obj->toXML($indent + 2, $path);
1468  }
1469 
1470  $xml .= str_repeat(" ", $indent) . " </$rel>\n";
1471  }
1472  else
1473  {
1474  $xml .= str_repeat(" ", $indent) . " <$rel/>\n";
1475  }
1476  }
1477  }
1478  }
1479 
1480  $xml .= str_repeat(" ", $indent) . "</" . get_class($this) . ">\n";
1481 
1482  return $xml;
1483  }
const SerializeDirectRelations
const SerializeRelations
const ProtectHTML

◆ unpack()

DataItem::unpack ( )

The unpack() method is called after populating a DataItem.

It can be used to unpack serialized sub-object representations so they are can be used directly by your application.

Definition at line 552 of file data_item.inc.

553  {
554  }

◆ update()

DataItem::update ( )

Update the row in the database that corresponds to this object.

Reimplemented from AbstractDataItem.

Definition at line 707 of file data_item.inc.

708  {
709  $this->fireEvent("beforeUpdate");
710  $this->pack();
711 
712  $pk = $this->primary_key;
713 
714  $query = "UPDATE {$this->table} SET ";
715 
716  $first = true;
717  $found = false;
718 
719  foreach($this->getFields() as $field => $type)
720  {
721  if ($field == $this->primary_key) continue;
722  if ($this->filter && $this->filter->isExcluded($field)) continue;
723  if (!isset($this->$field) && $this->getType($field) != Timestamp && $this->getType($field) != Boolean) continue;
724 
725  if (!$first) $query .= ", ";
726 
727  $first = false;
728  $found = true;
729 
730  $query .= "`$field`=";
731  $query .= $this->quoteFieldValue($field, $type);
732  }
733 
734  if (!$found) return true; // No fields to update - bug out quietly
735 
736  $query .= " WHERE $pk={$this->$pk} ".$this->getIdentityConstraint();
737 
738  trace("DataItem::update() - $query", 3);
739 
740  try
741  {
742 
743  $db = $this->getConnection();
744 
745  $success = $db->exec($query);
746  if ($success === FALSE)
747  {
748  trace("DataItem::update failed - $query", 2);
749  }
750  else
751  {
752  $this->fireEvent("onUpdate");
753  }
754  }
755  catch(PDOException $e)
756  {
757  $err = "DataItem::update() failed - " . $e->getMessage();
758  trace($err, 2);
759  throw new DataItemException($err);
760  }
761 
762  return $success;
763  }

◆ updateExplicit()

DataItem::updateExplicit (   $updates,
  $params = null 
)

Executes an explicit update command against the database.

This enables you to perform updates on the object using database functions, allowing for atomic operations, etc. For example, you might make such a call as $obj->updateExplicit("SET count=count+1"); The object is refreshed from the database automatically after the update.

Parameters
string$updatesthe update command to be run
array$paramsoptional array for bound parameters
Returns
boolean TRUE if the update was successful, FALSE if not.

Definition at line 775 of file data_item.inc.

776  {
777  $pk = $this->primary_key;
778 
779  $query = "UPDATE {$this->table} $updates";
780 
781  if ($this->get($pk))
782  {
783  $query .= " WHERE {$pk}=".$this->get($pk);
784  }
785 
786  trace("DataItem::update() - $query", 3);
787 
788  try
789  {
790  $db = $this->getConnection();
791  $stmt = $db->prepare($query);
792  $success = $stmt->execute($params);
793  if ($success === FALSE)
794  {
795  trace("DataItem::updateExplicit failed - $query", 2);
796  }
797  if ($this->get($pk)) $this->select();
798  }
799  catch(PDOException $e)
800  {
801  $err = "DataItem::updateExplicit() failed - " . $e->getMessage();
802  trace($err, 2);
803  throw new DataItemException($err);
804  }
805 
806  return $success;
807  }
select()
Select the object from the database, based on the value of the primary key field.
Definition: data_item.inc:656

Member Data Documentation

◆ $_eventMap

DataItem::$_eventMap = array()
static

Definition at line 63 of file data_item.inc.

◆ $_pkDisabled

DataItem::$_pkDisabled = false

Definition at line 66 of file data_item.inc.

◆ $_tx

DataItem::$_tx = null

Definition at line 65 of file data_item.inc.

◆ $dataTypeRendererMap


The documentation for this class was generated from the following file: