38 require_once realpath(dirname(__FILE__).
"/abstract_data_item.inc");
39 require_once realpath(dirname(__FILE__).
"/query.inc");
40 require_once realpath(dirname(__FILE__).
"/indexed_query.inc");
41 require_once realpath(dirname(__FILE__).
"/grouped_query.inc");
42 require_once realpath(dirname(__FILE__).
"/iterated_query.inc");
43 require_once realpath(dirname(__FILE__).
"/json_query.inc");
99 if (func_num_args() > 0)
101 $arg = func_get_arg(0);
109 else if (count($arg) > 0)
111 if (is_array($arg[0]))
117 $this->
load($arg[0]);
123 if (func_num_args() > 1)
125 $this->filter = func_get_arg(1);
138 array_push($f,
"primary_key",
"table",
"filter");
153 $eventKey =
"{$class}_{$event}";
171 $eventKey = get_class($this).
"_{$event}";
177 trace(
"===== DataItem event key = $eventKey", 3);
180 if (!is_array($handlers))
return;
181 foreach($handlers as $handler)
183 if (!is_callable($handler))
185 trace(get_class($this).
" {$event} handler is invalid", 2);
189 call_user_func_array($handler, array($this));
221 if (get_class($this) != $class)
231 if (!$this->_disablePK)
234 $conn->prepare(
"ALTER TABLE `{$this->table}` DISABLE KEYS")->execute();
235 $this->_disablePK =
true;
241 if ($this->_disablePK)
244 $conn->prepare(
"ALTER TABLE `{$this->table}` ENABLE KEYS")->execute();
245 $this->_disablePK =
false;
251 return $this->hiddenFields;
273 $offset = strlen($alias) + 1;
275 foreach ($line as $field => $value)
278 $field = substr($field, $offset);
280 if (!array_key_exists($field, $fields))
continue;
281 if ($this->filter && $this->filter->isExcluded($field))
continue;
283 if (!isset($config[
"no_charset_conversions"]) || $config[
"no_charset_conversions"])
285 $this->$field = $value;
289 $this->$field = iconv(
"UTF-8",
"CP1252//IGNORE", $value);
295 foreach ($line as $field => $value)
297 if (!array_key_exists($field, $fields))
continue;
298 if ($this->filter && $this->filter->isExcluded($field))
continue;
300 if (!isset($config[
"no_charset_conversions"]) || $config[
"no_charset_conversions"])
302 $this->$field = $value;
306 $this->$field = iconv(
"UTF-8",
"CP1252//IGNORE", $value);
311 if (isset($this->calculatedFields))
313 foreach($this->calculatedFields as $field => $expr)
317 $this->$field = $line[$alias.
".".$field];
321 $this->$field = $line[$field];
335 if ($this->fields)
return $this->fields;
336 $class = get_class($this);
337 if ($class::$fields)
return $class::$fields;
339 throw new FakoliException(
"DataItem does not contain any fields");
349 if (!is_array($this->fields))
351 throw new FakoliException(
"Cannot override field types for statically defined DataItems");
354 $this->fields[$field] = $type;
366 foreach($this->
getFields() as $field => $type)
368 if ($this->filter && $this->filter->isExcluded($field))
continue;
369 if (!$first) $list .=
", ";
374 $list .=
"`".$field.
"`";
377 $list .=
" as `$alias.$field`";
382 if (isset($this->calculatedFields))
384 foreach($this->calculatedFields as $field => $expr)
386 if ($this->filter && $this->filter->isExcluded($field))
continue;
387 if (!$first) $list .=
", ";
391 foreach($this->
getFields() as $f => $type)
393 $expr = preg_replace(
'/([\s\(])('.$f.
')\b/',
"$1{$alias}.$2", $expr);
394 $expr = preg_replace(
'/\b'.$this->table.
'\.'.$f.
'\b/',
"{$alias}.{$f}", $expr);
398 $list .= $expr .
" as `";
418 foreach($this->
getFields() as $field => $type)
420 if ($this->filter && $this->filter->isExcluded($field))
continue;
436 if (!array_key_exists($field, $this->
getFields()))
return false;
437 if ($this->filter && $this->filter->isExcluded($field))
return false;
449 return (!array_key_exists($relation, $this->relations)) ? false :
true;
457 return $this->primary_key;
466 return array($this->primary_key);
476 if (!$field)
throw new FakoliException(
"Cannot access empty field");
477 return $this->$field;
485 function set($field, $value)
487 if (!$field)
throw new FakoliException(
"Cannot access empty field");
488 $this->$field = $value;
499 return $fields[$field];
525 return $this->fieldAliases;
534 return $this->fieldAnnotations;
566 $query =
"SELECT $fields FROM {$this->table} WHERE {$this->primary_key}=$id ".$this->getIdentityConstraint();
567 trace(
"DataItem::load($id): $query", 3);
573 $result = $db->prepare($query);
576 if ($line = $result->fetch())
583 catch(PDOException $e)
585 $err =
"DataItem::load() failed - ".$e->getMessage();
604 if (!$pk)
return null;
606 $cl = get_class($this);
622 if (!$this->
hasField(
"composite_class") || !$this->composite_class)
624 throw new DataItemException(
"Cannot create composite from ".get_class($this).
" as required composite cannot be determined");
627 $composite =
new $this->composite_class;
628 $composite->loadFromBase($this->
get($this->
getPrimaryKey()));
638 if ($this->cacheLocal)
658 $pk = $this->primary_key;
659 $this->
load($this->$pk);
668 $pk = $this->primary_key;
672 if ($this->$pk ==
"" || $this->$pk == 0)
return false;
673 $constraint =
"WHERE $pk={$this->$pk}";
676 $query =
"SELECT $pk FROM {$this->table} $constraint";
677 trace(
"DataItem::exists() - $query", 3);
684 $result = $db->prepare($query);
687 if ($line = $result->fetch())
694 catch(PDOException $e)
696 $err =
"DataItem::exists() failed - " . $e->getMessage();
712 $pk = $this->primary_key;
714 $query =
"UPDATE {$this->table} SET ";
719 foreach($this->
getFields() as $field => $type)
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;
725 if (!$first) $query .=
", ";
730 $query .=
"`$field`=";
734 if (!$found)
return true;
736 $query .=
" WHERE $pk={$this->$pk} ".$this->getIdentityConstraint();
738 trace(
"DataItem::update() - $query", 3);
745 $success = $db->exec($query);
746 if ($success === FALSE)
748 trace(
"DataItem::update failed - $query", 2);
755 catch(PDOException $e)
757 $err =
"DataItem::update() failed - " . $e->getMessage();
777 $pk = $this->primary_key;
779 $query =
"UPDATE {$this->table} $updates";
783 $query .=
" WHERE {$pk}=".$this->get($pk);
786 trace(
"DataItem::update() - $query", 3);
791 $stmt = $db->prepare($query);
792 $success = $stmt->execute($params);
793 if ($success === FALSE)
795 trace(
"DataItem::updateExplicit failed - $query", 2);
797 if ($this->
get($pk)) $this->
select();
799 catch(PDOException $e)
801 $err =
"DataItem::updateExplicit() failed - " . $e->getMessage();
818 $pk = $this->primary_key;
822 $timestampField =
null;
824 foreach($this->
getFields() as $field => $type)
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;
830 if ($type == Timestamp)
832 $timestampField = $field;
841 $fields .=
"`".$field.
"`";
847 $query =
"INSERT INTO {$this->table} ($fields) values ($values)";
849 trace(
"DataItem::insert() - $query", 3);
856 $success = $db->exec($query);
858 if ($success !== FALSE)
860 $this->$pk = $db->lastInsertId();
864 $this->$timestampField = $this->__timestamp;
865 unset($this->__timestamp);
873 trace(
"DataItem::insert() failed - $query", 2);
876 catch(PDOException $e)
878 $err =
"DataItem::insert() failed - ". $e->getMessage();
888 function delete($constraint =
"")
890 if ($constraint ==
"")
893 $pk = $this->primary_key;
894 $constraint =
"WHERE $pk={$this->$pk} ".$this->getIdentityConstraint();
897 $query =
"DELETE FROM {$this->table} $constraint";
905 if ($fire) $this->
fireEvent(
"onPreDelete");
907 if ($fire) $this->
fireEvent(
"onPostDelete");
909 catch(PDOException $e)
911 $err =
"DataItem::delete() failed - " . $e->getMessage();
923 $query =
"DELETE FROM {$this->table}; ALTER TABLE {$this->table} AUTO_INCREMENT=1";
933 catch(PDOException $e)
935 $err =
"DataItem::deleteAll() failed - " . $e->getMessage();
946 $query =
"SHOW TABLES LIKE '{$this->table}'";
955 $result = $db->prepare($query);
958 if ($line = $result->fetch())
965 catch(PDOException $e)
967 $err =
"DataItem::tableExists() failed - " . $e->getMessage();
985 $pk = $this->primary_key;
986 $old = $this->
get($pk);
1003 $query =
"SELECT DISTINCT $field from {$this->table} $constraint";
1011 $result = $db->prepare($query);
1016 while($line = $result->fetch())
1018 $values[] = $line[$field];
1030 catch(PDOException $e)
1032 $err =
"DataItem::distinctValues() failed - " . $e->getMessage();
1044 foreach($this->
getFields() as $field => $type)
1046 if ($this->filter && $this->filter->isExcluded($field))
continue;
1047 if (array_key_exists($field, $_GET))
1049 $this->
set($field, $_GET[$field]);
1064 foreach($this->
getFields() as $field => $type)
1066 if ($this->filter && $this->filter->isExcluded($field))
continue;
1067 if (array_key_exists($field, $_POST))
1069 $this->
set($field, $_POST[$field]);
1071 elseif (!array_key_exists($field, $_POST) AND ($type == Boolean))
1086 switch($_SERVER[
"REQUEST_METHOD"])
1104 foreach($this->
getFields() as $field => $type)
1106 if ($this->filter && $this->filter->isExcluded($field))
continue;
1107 if (array_key_exists($field, $params))
1109 $this->$field = $params[$field];
1111 elseif (!array_key_exists($field, $params) AND ($type == Boolean))
1129 foreach($this->
getFields() as $field => $type)
1131 if ($this->filter && $this->filter->isExcluded($field))
continue;
1132 if ($to->$field != $this->$field)
1134 trace(
"$field '{$this->$field}' != '{$to->$field}'", 3);
1150 foreach($this->
getFields() as $field => $type)
1152 if ($this->filter && $this->filter->isExcluded($field))
continue;
1153 $this->
set($field, $from->$field);
1156 if (is_array($this->calculatedFields))
1158 foreach($this->calculatedFields as $field => $expr)
1160 $this->
set($field, $from->$field);
1175 $pk = $this->primary_key;
1181 if ($this->cacheLocal)
1183 $cache = get_class($this).
"_cache";
1185 if (!is_array($items))
1190 if (array_key_exists($id, $items))
1192 trace(
"Using local cache $cache for item $id", 5);
1193 $this->
copy($items[$id]);
1215 $field = $obj->primary_key;
1219 if (array_key_exists($field, $this->
getFields()))
1221 $pk = $obj->primary_key;
1222 $val = $this->
get($field);
1224 if (!$val)
return null;
1228 $cache =
"_{$class}_{$field}_{$val}";
1229 if (isset($this->$cache))
1231 $obj->copy($this->$cache);
1236 $this->$cache = $obj;
1241 $field = $this->primary_key;
1242 $obj =
querySingle($class,
"WHERE $field={$this->$field}");
1262 $pk = $this->primary_key;
1264 if ($field ==
"") $field = $pk;
1265 $value = $this->$field ? $this->$field : $this->$pk;
1266 $orderBy = preg_replace(
"/^\s*WHERE\s+/i",
"AND ", $orderBy);
1268 $list =
query($class,
"WHERE $field=$value $orderBy");
1286 function crossReference($class, $xref, $orderBy =
"", $pkField =
"", $xrefField =
"")
1293 foreach($obj->getFields() as $field => $type)
1295 $orderBy = preg_replace(
"/\\b".$field.
"\\b/i",
"a0.$field", $orderBy);
1299 $orderBy = preg_replace(
"/^\s*WHERE\s+/i",
"AND ", $orderBy);
1302 $xp = ($xrefField !=
"") ? $xrefField : $obj->primary_key;
1304 $pkField = ($pkField !=
"") ? $pkField : $this->primary_key;
1305 $pk = $this->primary_key;
1307 $fieldList = $obj->getFieldList(
"a0");
1309 $query =
"SELECT {$fieldList} FROM {$obj->table} a0, $xt x WHERE a0.{$obj->primary_key}=x.{$xp} AND x.{$pkField}={$this->$pk} $orderBy";
1317 $result = $db->prepare($query);
1321 while ($line = $result->fetch())
1324 $obj->populate($line,
"a0");
1330 catch(PDOException $e)
1332 $err =
"DataItem::crossReference() failed - " . $e->getMessage();
1351 if (func_num_args() > 1)
1353 $constraints = func_get_arg(1);
1355 if (func_num_args() > 2)
1357 $value = func_get_arg(2);
1361 if ($constraints ==
"") $constraints =
"WHERE 1=1";
1362 $constraints .=
" ".$this->getIdentityConstraint();
1364 $query =
"SELECT $func as result FROM {$this->table} $constraints";
1366 trace(
"DataItem::queryValue: $query", 3);
1370 $result = $db->prepare($query);
1373 if ($row =$result->fetch())
1375 $value = $row[
'result'];
1380 catch(PDOException $e)
1382 $err =
"DataItem::queryValue() failed - " . $e->getMessage();
1396 function toXML($indent = 0, $path =
null)
1398 trace(get_class($this).
"->toXML()", 4);
1400 $xml = str_repeat(
" ", $indent) .
"<" . get_class($this) .
">\n";
1402 foreach($this->
getFields() as $field => $type)
1404 if ($this->filter && $this->filter->isExcluded($field))
continue;
1405 if (isset($this->$field))
1407 if ($this->$field ===
"")
1409 $xml .= str_repeat(
" ", $indent) .
" <$field/>\n";
1413 $xml .= str_repeat(
" ", $indent) .
" <{$field}>".$this->formatFieldForXML($field).
"</$field>\n";
1419 $path[get_class($this)] =
true;
1420 trace(implode(
" > ", array_keys($path)), 4);
1421 trace(
"Options: ".$this->_options, 4);
1427 if (is_array($this->relations))
1429 foreach($this->relations as $rel => $relType)
1431 if (is_array($path) && array_key_exists($relType, $path))
continue;
1433 $obj = $this->$rel();
1436 $xml .= str_repeat(
" ", $indent) .
" <$rel>\n";
1440 foreach($obj as $item)
1452 $xml .= $item->toXML($indent + 2, $path);
1467 $xml .= $obj->toXML($indent + 2, $path);
1470 $xml .= str_repeat(
" ", $indent) .
" </$rel>\n";
1474 $xml .= str_repeat(
" ", $indent) .
" <$rel/>\n";
1480 $xml .= str_repeat(
" ", $indent) .
"</" . get_class($this) .
">\n";
1487 if ($node->nodeType != XML_ELEMENT_NODE || $node->tagName != get_class($this))
return;
1489 $kids = $node->childNodes;
1490 $numChildren = $kids->length;
1492 for($i = 0; $i < $numChildren; ++$i)
1494 $n = $kids->item($i);
1496 if ($n->nodeType != XML_ELEMENT_NODE)
continue;
1497 $field = $n->tagName;
1498 $this->
set($field, $n->nodeValue);
1506 foreach($this->
getFields() as $field => $type)
1508 if ($this->filter && $this->filter->isExcluded($field))
1513 $val = str_replace(
"\\'",
"'", json_encode((
string)$this->
get($field)));
1515 $out[] =
"\"{$field}\": ".$val;
1518 return "{".implode(
", ", $out).
"}";
1582 function format($template =
"", $separator =
", ")
1584 if ($template ==
"") $template = $this->default_format;
1585 if (is_array($template) && is_callable($template))
1587 return call_user_func($template, $this);
1590 foreach($this->
getFields() as $field => $type)
1592 $template = str_replace(
"{".$field.
"}", $this->formatFieldValue($field), $template);
1595 if (isset($this->calculatedFields))
1597 foreach($this->calculatedFields as $field => $expr)
1599 $template = str_replace(
"{".$field.
"}", $this->$field, $template);
1603 if (isset($this->_decorations))
1605 foreach($this->_decorations as $field => $value)
1607 $template = str_replace(
"{".$field.
"}", $value, $template);
1620 preg_match_all(
"/\\{([\\w_]+)\\(\\)\\}/", $template, $matches, PREG_SET_ORDER);
1622 foreach($matches as $match)
1624 $format = $match[0];
1625 $method = $match[1];
1627 $value = $this->$method();
1629 $template = str_replace($format, $value, $template);
1647 preg_match_all(
"/\\{([\\w\\d_]+)(::?|\\^|\\(\\)|\\|)?([^}]*)}/", $template, $matches, PREG_SET_ORDER);
1649 foreach($matches as $match)
1653 $separator = ($match[2]) ? $match[2] :
":";
1655 $fieldFormat = $match[3];
1656 trace(
"formatField, template $template, field $field, separator $separator and fieldFormat $fieldFormat", 5);
1658 $format =
"{".$field.$separator.$fieldFormat.
"}";
1663 if (!$this->
get($field))
1665 $sub = $fieldFormat;
1674 if (!$this->
get($field))
1676 $sub = $this->
format(
"{".$fieldFormat.
"}");
1686 $sub = call_user_func(array($field, $fieldFormat), $this);
1691 $sub = $this->$field();
1699 $template = str_replace($format, $sub, $template);
1716 preg_match_all(
"/\\{([\\w\\d_]+)\\.([\\w\\d_\\.\\(\\)]+)([:\\|\\^])?([^}]*)}/", $template, $matches, PREG_SET_ORDER);
1718 foreach($matches as $match)
1720 $relation = $match[1];
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];
1728 trace(
"function format: formatThroughRelations template $template field $field relation $relation constraint $constraint", 5);
1732 $relations = $this->$relation($constraint);
1733 $format =
"{".$relation.
".".$field.$divider.$constraint.
"}";
1735 elseif($fieldTemplate)
1737 $relations = $this->$relation();
1738 $format =
"{".$relation.
".".$field.$divider.$fieldTemplate.
"}";
1742 $relations = $this->$relation();
1743 $format =
"{".$relation.
".".$field.
"}";
1748 if (is_array($relations))
1750 $formatTemplate = ($fieldTemplate) ?
"{".$field .
":".$fieldTemplate .
"}" :
"{".$field.
"}";
1751 $sub =
formatItems($relations, $formatTemplate, $separator);
1754 else if ($relations)
1756 if ($fieldTemplate) $fieldTemplate =
":{$fieldTemplate}";
1757 if ($relations) $sub = $relations->format(
"{{$field}{$fieldTemplate}}");
1760 trace(
"format using relations: Replacing {$match[0]} [ $format ] with '$sub'", 5);
1762 $template = str_replace($format, $sub, $template);
1771 if (isset($this->fieldAliases) && array_key_exists($field, $this->fieldAliases))
1773 return $this->fieldAliases[$field];
1776 $field = preg_replace(
"/([a-z])([A-Z0-9])/",
"$1 $2", $field);
1777 $field = str_replace(
"_",
" ", $field);
1778 $field = ucwords($field);
1793 $pk = $target->primary_key;
1795 if (!$field) $field = $pk;
1797 $this->$field = $target->$pk;
1814 if (func_num_args() > 1)
1816 $constraints = func_get_arg(1);
1819 $result =
query($class, $constraints);
1821 if (count($result) > 1)
1826 if (count($result) == 1)
1847 if (func_num_args() > 2)
1849 $constraints = func_get_arg(2);
1851 if (func_num_args() > 3)
1853 $value = func_get_arg(3);
1857 $prototype =
new $class;
1859 if ($constraints ==
"") $constraints =
"WHERE 1=1";
1860 $constraints .=
" ".$prototype->getIdentityConstraint();
1862 $query =
"SELECT $func as result FROM {$prototype->table} $constraints";
1870 $result = $db->prepare($query);
1873 if ($row = $result->fetch())
1875 $value = $row[
'result'];
1880 catch(PDOException $e)
1882 $err =
"queryValue() failed - " . $e->getMessage();
1902 foreach($list as $elt)
1908 if (array_key_exists($e->$field, $result))
1910 if (!is_array($result[$e->$field]))
1912 $result[$e->$field] = array($result[$e->$field]);
1914 $result[$e->$field][] = $e;
1918 $result[$e->$field] = $e;
1924 if (array_key_exists($elt->$field, $result) && $autoPromote)
1926 if (!is_array($result[$elt->$field]))
1928 $result[$elt->$field] = array($result[$elt->$field]);
1930 $result[$elt->$field][] = $elt;
1934 $result[$elt->$field] = $elt;
1952 foreach($list as $elt)
1956 foreach ($elt as $e)
1958 $result[$e->$field][] = $e;
1963 $result[$elt->$field][] = $elt;
1983 if (count($list) == 0)
return $list;
1987 $field = $list[0]->primary_key;
1991 $filtered = array();
1992 foreach($list as $item)
1994 if (!array_key_exists($item->$field, $found))
1996 $filtered[] = $item;
1999 $found[$item->$field] =
true;
2020 return ($a->get($this->field) < $b->get($this->field)) ? -1 : 1;
2034 usort($list, array($cmp,
"compare"));
2048 if (!$params) $params = $_GET;
2050 $pk = $obj->getPrimaryKey();
2056 foreach($params[$pk] as $id => $sort_order)
2061 $item =
new $class();
2062 $item->joinTransaction($tx);
2067 $item->set($field, $sort_order);
2080 function toXML($tag, $objects, $header =
null)
2084 if ($header ===
null)
2086 $default_charset = $config[
"default_charset"];
2087 if (!$default_charset) $default_charset =
"iso-8859-1";
2088 $header =
"<?xml version=\"1.0\" encoding=\"{$default_charset}\"?>";
2091 $xml =
"$header\n<$tag>\n";
2093 foreach($objects as $obj)
2095 $xml .= $obj->toXML(1);
2097 $xml .=
"</$tag>\n";
2106 foreach($items as $item)
2108 $out[] = $item->toJSON();
2111 return "[".implode(
", ", $out).
"]";
2118 foreach($items as $key => $group)
2120 $out[] =
"\"$key\": ".toJSON($group);
2123 return "{".implode(
", ", $out).
"}";
2130 foreach($items as $key => $item)
2132 $out[] =
"\"$key\": ".$item->toJSON();
2135 return "{".implode(
", ", $out).
"}";
2140 $data = json_decode($json);
2142 if (is_object($data))
2148 else if (is_array($data))
2150 foreach($data as $datum)
2165 return formatItems($items,
"{".$field.
"}", $separator);
2181 foreach($items as $item)
2183 if (!$item)
continue;
2184 if (!$first) $list .= $separator;
2185 $list .= $item->format($template);
2195 foreach($items as $item)
2197 $list[$item->format($indexFormat)] = $item->format($valueFormat);
2212 foreach($items as $item)
2214 $out[] = $item->get($field);
2229 foreach($items as $item)
2231 $out[$item->get($item->getPrimaryKey())] = $item->get($field);
2250 $obj->set($obj->getPrimaryKey(), $key);
2251 return $obj->exists();
2263 foreach($items as $item)
2265 if ($item->get($field) == $value)
return $item;
2283 foreach($items as $item)
2285 if ($item->get($field) == $value)
return $idx;
2294 $v = str_replace( array(
"[_[OPENTAG]_]",
"[_[CLOSETAG]_]",
"[_[AMPERSAND]_]"), array(
"<",
">",
"&"), $text);
2304 function dumpCSV($class, $items, $filepath, $filter =
null, $append =
false)
2307 $obj->filter = $filter;
2309 $fields = $obj->getFieldArray();
2313 $fp = fopen($filepath,
"a");
2317 $fp = fopen($filepath,
"w");
2318 fputcsv($fp, $fields);
2321 foreach($items as $item)
2324 foreach($fields as $field)
2326 $vals[] = $item->get($field);
2328 fputcsv($fp, $vals);
Abstract base class for all DataItem implementations.
getConnection()
Retrieves a connection to the database.
formatFieldValue($field, $template="")
quoteFieldValue($field, $type=null)
Returns the properly quoted value of the specified field.
getPrimaryKeyValue()
Retrieves the primary key value.
static invalidate($key)
Invalidates the specifed entry in the cache.
static get($key)
Retrieve the specified object from the cache.
static put($key, $obj, $ttl=0)
Store the specified object in the cache at the specified key.
static getConnection()
Retrieves a reference to the global database connection.
Simple field comparator to support sorting arrays of DataItems by the values in a specified field.
DataItem is the generic base class for database mapped classes.
load($id)
Load the object with the specified primary key.
getFields()
Retrieve the field type list for this object.
fromXML($node)
Populates the object from the specified XML node.
getFilter()
Returns the filter set on this object.
getHiddenFields()
Retrieves the list of hidden fields.
fromPOST()
Automatically populate the object based on parameters in the $_POST collection.
distinctValues($field, $sorted=false, $constraint="")
Retrieves the distinct values in the database for the specified field across the specified set of rec...
prettifyFieldName($field)
cast($class)
Cast this object to another class.
static $dataTypeRendererMap
duplicate()
Persists a duplicate of the DataItem to the database under a new primary key.
cacheLookup($id)
Populate the object from the local cache if the object is marked as "cacheLocal".
exists($constraint="")
Check whether the object exists in the database.
compare($to)
Compare this object to another object.
relateTo($target, $field="")
Link this object to the specified target by setting corresponding field to the value of the target's ...
cloneFrom($from)
Copies values from another object, field by field, excluding the primary key.
hasField($field)
Returns true if this DataItem contains a field with the specified name and that field is not excluded...
old()
Retrieves the currently stored state of the DataItem from the database and returns it as a new object...
getTransaction()
Retrieves the current DataTransaction.
fireEvent($event)
Fires the specified event to all registered handlers.
getRelated($class, $field="")
Returns a single item related by the specified foreign key.
deleteAll()
Delete all the rows in the database that correspond to this class.
format($template="", $separator=", ")
Substitute values for field names in a string, with the fields formatted using their type's default f...
updateExplicit($updates, $params=null)
Executes an explicit update command against the database.
joinTransaction($tx)
Join the DataItem to the specified DataTransaction.
fromGET()
Automatically populate the object based on parameters in the $_GET collection.
copy($from)
Copies values from another object, field by field.
toXML($indent=0, $path=null)
Generates an XML representation of the object.
queryValue($func)
Query the database to calculate an aggregate value.
getRelatedList($class, $field="", $orderBy="")
Returns a list of items that are related to this item.
tableExists()
Check if the table for this class exists in the database.
getFieldList($alias="")
Returned a comma-separated list of the fields for this object (applying the assigned filter if there ...
getPrimaryKeyList()
Retrieves a list of all the primary keys used for an object as an array.
getFieldAnnotations()
Retrieve the list of field annotations.
getType($field)
Retrieves the data type of the specified field.
static registerEventHandler($class, $event, $handler)
Register an event handler for a specific event and DataItem class.
populate($line, $alias=false)
Populates the object using the supplied associative array (field -> value).
unpack()
The unpack() method is called after populating a DataItem.
fromREQUEST()
Automatically populate the object based on parameters in either the $_GET or $_POST collection,...
getPrimaryKey()
Retrieves the primary key field name.
insert()
Insert a new row in the database to store this object.
setFilter($filter)
Sets the filter on this object.
fromDataSet($params)
Automatically populate the object based on a custom set of parameters (such as a filtered $_POST coll...
pack()
The pack() method is called prior to persisting a DataItem to storage.
crossReference($class, $xref, $orderBy="", $pkField="", $xrefField="")
Returns a list of items related to this item via a cross-reference table.
hasRelation($relation)
Returns true if this DataItem contains a relation with the specified name.
getFieldArray()
Return an array of field names for this object filtered by any active filter.
select()
Select the object from the database, based on the value of the primary key field.
overrideFieldType($field, $type)
Override the type for the specified field.
formatThroughRelation($template, $separator=", ")
save()
Store the object in the database.
loadComposite()
Creates an outer CompositeDataItem from its base component.
getFieldAliases()
Retrieve the list of field aliases.
update()
Update the row in the database that corresponds to this object.
The DataTransaction class wraps the underlying database's transaction model.
Used to place a filter on the contents of a DataItem-derived object.
static create($class, $constraints="", $indexBy="")
checkNumeric($p)
Security helper function.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
itemExists($class, $key)
Utility method to test for the existence of a DataItem record in the database.
updateSortOrder($class, $field="sort_order", $params=null)
Utility method to update sort order for the selected class of objects based on an array that maps pri...
const SerializeDirectRelations
reindexList($list, $field, $autoPromote=true)
Reindex an indexed or grouped query result by a different field.
regroupList($list, $field)
Regroup an indexed or grouped query result by a different field.
findItem($items, $field, $value)
Find the first item in the array that matches the supplied value for the specified field.
extractIndexedFieldValues($items, $field)
Extract the field values for the specified field from a list of DataItems and return them in an array...
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
findItemIndex($items, $field, $value)
Find the index of the first item in the array that matches the supplied value for the specified field...
dumpCSV($class, $items, $filepath, $filter=null, $append=false)
Dump a raw representation of a list of DataItems to CSV (one column per field, default format only)
toXML($tag, $objects, $header=null)
extractFieldValues($items, $field)
Extract the field values for the specified field from a list of DataItems.
displayFieldAsList($items, $field, $separator=", ")
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
removeDuplicates($list, $field="")
Remove duplicate DataItems from a list, based on the value of a field.
querySingle($class)
Performs a query against the database and returns a matching singleton object.
queryValue($class, $func)
Query the database to calculate an aggregate value.
extractItems($items, $indexFormat, $valueFormat)
sortList(&$list, $field)
Sorts an array of DataItems based on the values in the specified field.