37 require_once realpath(dirname(__FILE__)).
"/data_item.inc";
38 require_once realpath(dirname(__FILE__)).
"/join.inc";
52 $args = func_get_args();
57 $pk = $obj->getPrimaryKey();
59 if (count($this->__objects))
63 foreach($this->__objects as $o)
65 if ($o->hasField($pk))
74 throw new DataItemException(
"$cl does not have a direct foreign key relationship with another class in ".get_class($this));
78 $this->__objects[] = $obj;
85 if ($this->$class)
return $this->$class;
96 foreach($this->__objects as $obj)
98 $obj->joinTransaction($tx);
124 $this->__objects[0]->populate($line);
125 $num = count($this->__objects);
127 for($i = 1; $i < num; ++$i)
129 $pk = $this->__objects[$i];
130 $key = $obj->get($pk);
144 $num = count($this->__objects);
145 for($i = $num - 1; $i >= 0; ++$i)
147 $of = $this->__objects[$i]->getFields();
148 $fields = array_merge($fields, $of);
151 return implode(
", ", array_keys($fields));
163 $num = count($this->__objects);
164 for($i = $num - 1; $i >= 0; --$i)
166 $of = $this->__objects[$i]->getFields();
167 $fields = array_merge($fields, $of);
170 return array_keys($fields);
180 $num = count($this->__objects);
181 for($i = $num - 1; $i >= 0; --$i)
183 $of = $this->__objects[$i]->getFields();
184 $fields = array_merge($fields, $of);
199 foreach($this->__objects as $obj)
201 if ($obj->hasField($field))
return true;
209 $hiddenFields = array();
211 foreach($this->__objects as $obj)
213 $hidden = $obj->getHiddenFields();
214 if ($hidden) array_merge($hiddenFields, $hidden);
217 return $hiddenFields;
228 foreach($this->__objects as $obj)
230 if ($obj->hasRelation($relation))
return true;
243 foreach($this->__objects as $obj)
245 if ($obj->hasField($field))
return $obj;
248 throw new FakoliException(
"Cannot locate field '$field' in sub-objects");
258 return $this->__objects[0]->getPrimaryKey();
269 foreach($this->__objects as $obj)
271 $pk = array_merge($pk, $obj->getPrimaryKeyList());
284 foreach($this->__objects as $obj)
286 if ($obj->hasField($field))
return $obj->get($field);
297 function set($field, $value)
299 foreach($this->__objects as $obj)
301 if ($obj->hasField($field)) $obj->set($field, $value);
312 foreach($this->__objects as $obj)
314 if ($obj->hasField($field))
return $obj->getType($field);
325 return $this->__objects[0]->getFilter();
334 foreach($this->__objects as $obj)
349 $num = count($this->__objects);
350 for($i = $num - 1; $i >= 0; --$i)
352 $of = $this->__objects[$i]->getFieldAliases();
355 $aliases = array_merge($aliases, $of);
371 $num = count($this->__objects);
372 for($i = $num - 1; $i >= 0; --$i)
374 $of = $this->__objects[$i]->getFieldAnnotations();
377 $aliases = array_merge($aliases, $of);
394 $num = count($this->__objects);
396 for($i = 0; $i < $num; ++$i)
398 $this->__objects[$i]->load($id);
400 if ($i == $num - 1)
continue;
402 $id = $this->__objects[$i]->get($this->__objects[$i+1]->
getPrimaryKey());
408 $num = count($this->__objects);
410 $this->__objects[$num - 1]->load($id);
412 for($i = $num - 2; $i >= 0; --$i)
415 $pk = $this->__objects[$i+1]->getPrimaryKey();
416 $cl = get_class($this->__objects[$i]);
417 $obj =
querySingle($cl,
"WHERE $pk=".$this->__objects[$i+1]->
get($pk));
418 $this->__objects[$i] = $obj;
430 $num = count($this->__objects);
432 for($i = $num - 1; $i >= 0; --$i)
434 if ($this->__objects[$i]->
hasField(
"composite_class"))
436 $this->__objects[$i]->set(
"composite_class", get_class($this));
439 $this->__objects[$i]->save();
442 $pk = $this->__objects[$i]->getPrimaryKey();
443 $this->__objects[$i - 1]->set($pk, $this->__objects[$i]->
get($pk));
456 $this->__objects[0]->select();
458 $num = count($this->__objects);
460 for($i = 1; $i < $num; $i)
462 $pk = $this->__objects[$i]->getPrimaryKey();
463 $this->__objects[$i]->set($pk, $this->__objects[$i - 1]->
get($pk));
464 $this->__objects[$i]->select();
473 return $this->__objects[0]->exists($constraint);
481 foreach($this->__objects as $obj)
494 $num = count($this->__objects);
496 for($i = $num - 1; $i >= 0; --$i)
498 $this->__objects[$i]->insert();
501 $pk = $this->__objects[$i]->getPrimaryKey();
502 $this->__objects[$i - 1]->set($pk, $this->__objects[$i]->
get($pk));
510 function delete($constraint =
"")
514 throw new DataItemException(
"CompositeDataItem::delete() does not currently support constraint clauses");
517 $num = count($this->__objects);
519 for($i = 0; $i < $num; ++$i)
521 $this->__objects[$i]->delete();
530 for($i = 0; $i < $num; ++$i)
532 $this->__objects[$i]->deleteAll();
546 foreach($this->__objects as $obj)
548 if ($obj->hasField($field))
550 return $obj->distinctValues($field, $sorted, $constraint);
554 throw new DataItemException(
"Could not find field '$field' in any component objects");
562 foreach($this->__objects as $obj)
576 foreach($this->__objects as $obj)
588 foreach($this->__objects as $obj)
603 if (get_class($to) != get_class($this))
return false;
604 $num = count($this->__objects);
606 for($i = 0; $i < $num; ++$i)
608 if (!$this->__objects[$i]->
compare($to->__objects[$i]))
return false;
622 if (get_class($to) != get_class($this))
return false;
624 $num = count($this->__objects);
626 for($i = 0; $i < $num; ++$i)
628 $this->__objects[$i]->copy($from->__objects[$i]);
637 function toXML($indent = 0, $path =
null)
639 $xml = str_repeat(
" ", $indent) .
"<" . get_class($this) .
">\n";
640 foreach ($this as $field => $val)
644 $xml .= $val->toXML($indent + 1);
648 $xml .= str_repeat(
" ", $indent) .
"</" . get_class($this) .
">\n";
659 function format($template =
"", $separator =
",")
663 preg_match_all(
"/\\{([\\w_]+)\.([^}]+)}/", $template, $matches, PREG_SET_ORDER);
665 foreach($matches as $match)
668 $subtemplate = $match[2];
670 $value = $this->$class->format(
"{".$subtemplate.
"}", $separator);
672 $template = str_replace($match[0], $value, $template);
681 foreach($this->__objects as $obj)
683 if ($obj->hasField($field))
return $obj->prettifyFieldName($field);
692 $c = ($this->pretty_class_name) ? $this->pretty_class_name : get_class($this);
693 $c = preg_replace(array(
"/([a-z])([A-Z0-9])/",
718 if ($field ==
"") $field = $target->getPrimaryKey();
720 foreach($this->__objects as $obj)
722 if ($obj->hasField($field))
724 $obj->relateTo($target, $field);
731 function query($constraints =
"", $page = -1, $size = -1)
734 foreach($this->__objects as $obj)
736 $join->add(get_class($obj));
739 $joinResults = $join->query($constraints, $page, $size);
743 foreach($joinResults as $result)
746 foreach($obj->__objects as $target)
748 $cl = get_class($target);
749 $target->copy($result->$cl);
775 list($idxClass, $field) = explode(
".", $indexBy);
779 $idxClass = $this->
class[0];
781 $field = $obj->primary_key;
784 $query = $this->generateQuery($constraints);
789 $result = $db->prepare($query);
794 while($line = $result->fetch())
798 for($i = 0; $i < count($this->classes); ++$i)
800 $class = $this->classes[$i];
801 $item->$class =
new $class;
802 $item->$class->populate($line);
805 $idx = $item->$idxClass->$field;
806 if (array_key_exists($idx, $items))
809 if (!is_array($items[$idx]))
811 $items[$idx] = array($items[$idx]);
813 $items[$idx][] = $item;
817 $items[$idx] = $item;
825 catch(PDOException $e)
827 die(
"InnerJoin::indexedQuery() failed - ".$e->getMessage());
844 list($idxClass, $field) = explode(
".", $indexBy);
848 $idxClass = $this->
class[0];
850 $field = $obj->primary_key;
853 trace(
"InnerJoin::groupedQuery(): Group by $idxClass $field", 3);
855 $query = $this->generateQuery($constraints);
860 $result = $db->prepare($query);
865 while($line = $result->fetch())
869 for($i = 0; $i < count($this->classes); ++$i)
871 $class = $this->classes[$i];
872 $item->$class =
new $class;
873 $item->$class->populate($line);
876 $idx = $item->$idxClass->$field;
877 $items[$idx][] = $item;
884 catch(PDOException $e)
886 die(
"InnerJoin::groupedQuery() failed - ".$e->getMessage());
Abstract base class for all DataItem implementations.
getPrimaryKeyList()
Retrieves a list of all the primary keys used for an object as an array.
findSubObject($field)
Finds and returns the sub-object that contains the given field.
deleteAll()
Delete all the rows in the database that correspond to this class.
groupedQuery($constraints="", $indexBy="")
Performs a query against the database, returning an array of arrays of CompositeDataItem objects of t...
getFieldAliases()
Retrieve the list of field aliases.
getHiddenFields()
Retrieves the list of hidden fields.
fromXML($node)
Populates the object from the specified XML node.
hasRelation($relation)
Returns true if this DataItem contains a relation with the specified name.
populate($line)
Populates the object using the supplied associative array (field -> value).
getType($field)
Retrieves the data type of the specified field.
hasField($field)
Returns true if this DataItem contains a field with the specified name and that field is not excluded...
fromREQUEST()
Automatically populate the object based on parameters in either the $_GET or $_POST collection,...
indexedQuery($constraints="", $indexBy="")
Performs a query against the database, returning an array of CompositeDataItem objects of the specifi...
copy($from)
Copies values from another object, field by field.
fromPOST()
Automatically populate the object based on parameters in the $_POST collection.
load($id)
Load the object with the specified primary key.
fromGET()
Automatically populate the object based on parameters in the $_GET collection.
getPrimaryKey()
Retrieves the primary key field name.
relateTo($target, $field="")
Link this object to the specified target by setting corresponding field to the value of the target's ...
format($template="", $separator=",")
getFieldAnnotations()
Retrieve the list of field annotations.
getFieldArray()
Return an array of field names for this object filtered by any active filter.
query($constraints="", $page=-1, $size=-1)
getFieldList($alias="")
Returned a comma-separated list of the fields for this object (applying the assigned filter if there ...
prettifyClassName($plural=false)
distinctValues($field, $sorted=false, $constraint="")
Retrieves the distinct values in the database for the specified field across the specified set of rec...
compare($to)
Compare this object to another object.
getFilter()
Returns the filter set on this object.
cast($class)
Cast this object to another class.
setFilter($filter)
Sets the filter on this object.
insert()
Insert a new row in the database to store this object.
update()
Update the row in the database that corresponds to this object.
prettifyFieldName($field)
joinTransaction($tx)
Join the DataItem to the specified DataTransaction.
getTransaction()
Retrieves the current DataTransaction.
select()
Select the object from the database, based on the value of the primary key field.
getFields()
Retrieve the field type list for this object.
toXML($indent=0, $path=null)
Generates an XML representation of the object.
save()
Store the object in the database.
exists($constraint="")
Check whether the object exists in the database.
static getConnection()
Retrieves a reference to the global database connection.
This class is used to programmatically perform inner join queries across multiple objects.
JoinResult is an empty placeholder class.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
pluralize($text, $count=0)
Takes a singular string and makes it plural.
querySingle($class)
Performs a query against the database and returns a matching singleton object.