Fakoli Framework
CompositeDataItem Class Reference
+ Inheritance diagram for CompositeDataItem:
+ Collaboration diagram for CompositeDataItem:

Public Member Functions

 CompositeDataItem ()
 
 add ()
 
 cast ($class)
 
 joinTransaction ($tx)
 Join the DataItem to the specified DataTransaction. More...
 
 getTransaction ()
 Retrieves the current DataTransaction. More...
 
 populate ($line)
 Populates the object using the supplied associative array (field -> value). 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...
 
 getFields ()
 Retrieve the field type list for this object. 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...
 
 getHiddenFields ()
 
 hasRelation ($relation)
 Returns true if this DataItem contains a relation with the specified name. More...
 
 findSubObject ($field)
 Finds and returns the sub-object that contains the given field. 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...
 
 load ($id)
 Load the object with the specified primary key. More...
 
 loadFromBase ($id)
 
 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...
 
 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...
 
 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...
 
 compare ($to)
 Compare this object to another object. More...
 
 copy ($from)
 Copies values from another object, field by field. More...
 
 toXML ($indent=0, $path=null)
 Generates an XML representation of the object. More...
 
 fromXML ($node)
 
 format ($template="", $separator=",")
 
 prettifyFieldName ($field)
 
 prettifyClassName ($plural=false)
 
 relateTo ($target, $field="")
 Link this object to the specified target by setting corresponding field to the value of the target's primary key. More...
 
 query ($constraints="", $page=-1, $size=-1)
 
 indexedQuery ($constraints="", $indexBy="")
 Performs a query against the database, returning an array of CompositeDataItem objects of the specified class, indexed by a particular field. More...
 
 groupedQuery ($constraints="", $indexBy="")
 Performs a query against the database, returning an array of arrays of CompositeDataItem objects of the specified class, grouped by a particular field. 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...
 
 cast ($class)
 Cast this object to another class. 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...
 
 joinTransaction ($tx)
 Join the DataItem to the specified DataTransaction. More...
 
 getTransaction ()
 Retrieves the current DataTransaction. 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...
 
 getFields ()
 Retrieve the field type list for this object. More...
 
 getFieldList ($alias="")
 Returns 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...
 
 getPrimaryKeyValue ()
 Retrieves the primary key value. More...
 
 set ($field, $value)
 Set the value of the specified field. More...
 
 getType ($field)
 Retrieves the data type of the specified field. More...
 
 getHiddenFields ()
 Retrieves the list of hidden fields. 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...
 
 load ($id)
 Load the object with the specified primary key. 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...
 
 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...
 
 cacheLookup ($id)
 Populate the object from the local cache if the object is marked as "cacheLocal". More...
 
 quoteFieldValue ($field, $type)
 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)
 
 insert ()
 Insert a new row in the database to store this object. 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...
 
 compare ($to)
 Compare this object to another object. More...
 
 copy ($from)
 Copies values from another object, field by field. 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...
 
 fromJSON ($json)
 Populate from the supplied JSON object. More...
 
 format ($template="", $separator=", ")
 
 prettifyFieldName ($field)
 
 prettifyClassName ($plural=false)
 
 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 Attributes

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

Detailed Description

Definition at line 37 of file composite_data_item.inc.

Member Function Documentation

◆ add()

CompositeDataItem::add ( )

Definition at line 47 of file composite_data_item.inc.

48  {
49  $args = func_get_args();
50 
51  foreach($args as $cl)
52  {
53  $obj = new $cl();
54  $pk = $obj->getPrimaryKey();
55 
56  if (count($this->__objects))
57  {
58  $found = false;
59 
60  foreach($this->__objects as $o)
61  {
62  if ($o->hasField($pk))
63  {
64  $found = true;
65  break;
66  }
67  }
68 
69  if (!$found)
70  {
71  throw new DataItemException("$cl does not have a direct foreign key relationship with another class in ".get_class($this));
72  }
73  }
74 
75  $this->__objects[] = $obj;
76  $this->$cl = $obj;
77  }
78  }

◆ cast()

CompositeDataItem::cast (   $class)

Definition at line 80 of file composite_data_item.inc.

81  {
82  if ($this->$class) return $this->$class;
83 
84  throw new DataItemException("Cannot cast from ".get_class($this)." to $class");
85  }

◆ compare()

CompositeDataItem::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

Definition at line 598 of file composite_data_item.inc.

599  {
600  if (get_class($to) != get_class($this)) return false;
601  $num = count($this->__objects);
602 
603  for($i = 0; $i < $num; ++$i)
604  {
605  if (!$this->__objects[$i]->compare($to->__objects[$i])) return false;
606  }
607 
608  return true;
609  }
compare($to)
Compare this object to another object.

◆ CompositeDataItem()

CompositeDataItem::CompositeDataItem ( )

Definition at line 42 of file composite_data_item.inc.

43  {
44  $__objects = array();
45  }

◆ copy()

CompositeDataItem::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

Definition at line 617 of file composite_data_item.inc.

618  {
619  if (get_class($to) != get_class($this)) return false;
620 
621  $num = count($this->__objects);
622 
623  for($i = 0; $i < $num; ++$i)
624  {
625  $this->__objects[$i]->copy($from->__objects[$i]);
626  }
627  }

◆ delete()

CompositeDataItem::delete (   $constraint = "")

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

Definition at line 507 of file composite_data_item.inc.

508  {
509  if ($constraint)
510  {
511  throw new DataItemException("CompositeDataItem::delete() does not currently support constraint clauses");
512  }
513 
514  $num = count($this->__objects);
515 
516  for($i = 0; $i < $num; ++$i)
517  {
518  $this->__objects[$i]->delete();
519  }
520  }

◆ deleteAll()

CompositeDataItem::deleteAll ( )

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

Definition at line 525 of file composite_data_item.inc.

526  {
527  for($i = 0; $i < $num; ++$i)
528  {
529  $this->__objects[$i]->deleteAll();
530  }
531  }

◆ distinctValues()

CompositeDataItem::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

Definition at line 541 of file composite_data_item.inc.

542  {
543  foreach($this->__objects as $obj)
544  {
545  if ($obj->hasField($field))
546  {
547  return $obj->distinctValues($field, $sorted, $constraint);
548  }
549  }
550 
551  throw new DataItemException("Could not find field '$field' in any component objects");
552  }

◆ exists()

CompositeDataItem::exists (   $constraint = "")

Check whether the object exists in the database.

Definition at line 468 of file composite_data_item.inc.

469  {
470  return $this->__objects[0]->exists($constraint);
471  }

◆ findSubObject()

CompositeDataItem::findSubObject (   $field)

Finds and returns the sub-object that contains the given field.

Parameters
string$fieldthe field for which to search

Definition at line 238 of file composite_data_item.inc.

239  {
240  foreach($this->__objects as $obj)
241  {
242  if ($obj->hasField($field)) return $obj;
243  }
244 
245  throw new FakoliException("Cannot locate field '$field' in sub-objects");
246  }

◆ format()

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

Definition at line 656 of file composite_data_item.inc.

657  {
658  $matches = array();
659 
660  preg_match_all("/\\{([\\w_]+)\.([^}]+)}/", $template, $matches, PREG_SET_ORDER);
661 
662  foreach($matches as $match)
663  {
664  $class = $match[1];
665  $subtemplate = $match[2];
666 
667  $value = $this->$class->format("{".$subtemplate."}", $separator);
668 
669  $template = str_replace($match[0], $value, $template);
670  }
671 
672  return $template;
673  }

◆ fromGET()

CompositeDataItem::fromGET ( )

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

Definition at line 557 of file composite_data_item.inc.

558  {
559  foreach($this->__objects as $obj)
560  {
561  $obj->fromGET();
562  }
563  }

◆ fromPOST()

CompositeDataItem::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.

Definition at line 571 of file composite_data_item.inc.

572  {
573  foreach($this->__objects as $obj)
574  {
575  $obj->fromPOST();
576  }
577  }

◆ fromREQUEST()

CompositeDataItem::fromREQUEST ( )

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

Definition at line 583 of file composite_data_item.inc.

584  {
585  foreach($this->__objects as $obj)
586  {
587  $obj->fromREQUEST();
588  }
589  }

◆ fromXML()

CompositeDataItem::fromXML (   $node)

Definition at line 651 of file composite_data_item.inc.

652  {
653  //TODO: Implement this
654  }

◆ get()

CompositeDataItem::get (   $field)

Retrieve the value for the specified field.

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

Definition at line 279 of file composite_data_item.inc.

280  {
281  foreach($this->__objects as $obj)
282  {
283  if ($obj->hasField($field)) return $obj->get($field);
284  }
285 
286  return null;
287  }

◆ getFieldAliases()

CompositeDataItem::getFieldAliases ( )

Retrieve the list of field aliases.

Returns
array the alias list for this object

Definition at line 341 of file composite_data_item.inc.

342  {
343  $aliases = array();
344 
345  // Create a composite field list from the inside out.
346  $num = count($this->__objects);
347  for($i = $num - 1; $i >= 0; --$i)
348  {
349  $of = $this->__objects[$i]->getFieldAliases();
350  if ($of)
351  {
352  $aliases = array_merge($aliases, $of);
353  }
354  }
355 
356  return $aliases;
357  }

◆ getFieldAnnotations()

CompositeDataItem::getFieldAnnotations ( )

Retrieve the list of field annotations.

Returns
array the annotation list for this object

Definition at line 363 of file composite_data_item.inc.

364  {
365  $aliases = array();
366 
367  // Create a composite field list from the inside out.
368  $num = count($this->__objects);
369  for($i = $num - 1; $i >= 0; --$i)
370  {
371  $of = $this->__objects[$i]->getFieldAnnotations();
372  if ($of)
373  {
374  $aliases = array_merge($aliases, $of);
375  }
376  }
377 
378  return $aliases;
379  }

◆ getFieldArray()

CompositeDataItem::getFieldArray ( )

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

Returns
array

Definition at line 155 of file composite_data_item.inc.

156  {
157  $fields[] = array();
158 
159  // Create a composite field list from the inside out.
160  $num = count($this->__objects);
161  for($i = $num - 1; $i >= 0; --$i)
162  {
163  $of = $this->__objects[$i]->getFields();
164  $fields = array_merge($fields, $of);
165  }
166 
167  return array_keys($fields);
168  }

◆ getFieldList()

CompositeDataItem::getFieldList (   $alias = "")

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

Definition at line 136 of file composite_data_item.inc.

137  {
138  $fields[] = array();
139 
140  // Create a composite field list from the inside out.
141  $num = count($this->__objects);
142  for($i = $num - 1; $i >= 0; ++$i)
143  {
144  $of = $this->__objects[$i]->getFields();
145  $fields = array_merge($fields, $of);
146  }
147 
148  return implode(", ", array_keys($fields));
149  }

◆ getFields()

CompositeDataItem::getFields ( )

Retrieve the field type list for this object.

Definition at line 173 of file composite_data_item.inc.

174  {
175  $fields = array();
176  // Create a composite field list from the inside out.
177  $num = count($this->__objects);
178  for($i = $num - 1; $i >= 0; --$i)
179  {
180  $of = $this->__objects[$i]->getFields();
181  $fields = array_merge($fields, $of);
182  }
183 
184  return $fields;
185  }

◆ getFilter()

CompositeDataItem::getFilter ( )

Returns the filter set on this object.

Definition at line 320 of file composite_data_item.inc.

321  {
322  return $this->__objects[0]->getFilter();
323  }

◆ getHiddenFields()

CompositeDataItem::getHiddenFields ( )

Definition at line 204 of file composite_data_item.inc.

205  {
206  $hiddenFields = array();
207 
208  foreach($this->__objects as $obj)
209  {
210  $hidden = $obj->getHiddenFields();
211  if ($hidden) array_merge($hiddenFields, $hidden);
212  }
213 
214  return $hiddenFields;
215  }

◆ getPrimaryKey()

CompositeDataItem::getPrimaryKey ( )

Retrieves the primary key field name.

Definition at line 251 of file composite_data_item.inc.

252  {
253  // Primary key field for the composite object is the primary key field of the outermost object.
254 
255  return $this->__objects[0]->getPrimaryKey();
256  }

◆ getPrimaryKeyList()

CompositeDataItem::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

Definition at line 262 of file composite_data_item.inc.

263  {
264  $pk = array();
265 
266  foreach($this->__objects as $obj)
267  {
268  $pk = array_merge($pk, $obj->getPrimaryKeyList());
269  }
270 
271  return $pk;
272  }

◆ getTransaction()

CompositeDataItem::getTransaction ( )

Retrieves the current DataTransaction.

Definition at line 104 of file composite_data_item.inc.

105  {
106  return $this->__tx;
107  }

◆ getType()

CompositeDataItem::getType (   $field)

Retrieves the data type of the specified field.

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

Definition at line 307 of file composite_data_item.inc.

308  {
309  foreach($this->__objects as $obj)
310  {
311  if ($obj->hasField($field)) return $obj->getType($field);
312  }
313 
314  return null;
315  }

◆ groupedQuery()

CompositeDataItem::groupedQuery (   $constraints = "",
  $indexBy = "" 
)

Performs a query against the database, returning an array of arrays of CompositeDataItem objects of the specified class, grouped by a particular field.

If the indexing field has unique values, indexedQuery() might be more appropriate.

Parameters
string$constraintsoptional constraint clause to apply to the query in the form "WHERE ... [ORDER BY ...]"
string$fieldoptional the name of the field by which to group the results

Definition at line 834 of file composite_data_item.inc.

835  {
836  $field = "";
837  $idxClass = "";
838 
839  if ($indexBy != "")
840  {
841  list($idxClass, $field) = explode(".", $indexBy);
842  }
843  else
844  {
845  $idxClass = $this->class[0];
846  $obj = new $class;
847  $field = $obj->primary_key;
848  }
849 
850  trace("InnerJoin::groupedQuery(): Group by $idxClass $field", 3);
851 
852  $query = $this->generateQuery($constraints);
853 
854  try
855  {
857  $result = $db->prepare($query);
858  $result->execute();
859 
860  $items = array();
861 
862  while($line = $result->fetch())
863  {
864  $item = new JoinResult($this->tag);
865 
866  for($i = 0; $i < count($this->classes); ++$i)
867  {
868  $class = $this->classes[$i];
869  $item->$class = new $class; //Hack to work around PHP's stupid implementation of get_class()
870  $item->$class->populate($line);
871  }
872 
873  $idx = $item->$idxClass->$field;
874  $items[$idx][] = $item;
875  }
876 
877  unset($result);
878 
879  return $items;
880  }
881  catch(PDOException $e)
882  {
883  die("InnerJoin::groupedQuery() failed - ".$e->getMessage());
884  }
885  }
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
static getConnection()
Retrieves a reference to the global database connection.
JoinResult is an empty placeholder class.
Definition: join.inc:42

◆ hasField()

CompositeDataItem::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.

Definition at line 194 of file composite_data_item.inc.

195  {
196  foreach($this->__objects as $obj)
197  {
198  if ($obj->hasField($field)) return true;
199  }
200 
201  return false;
202  }

◆ hasRelation()

CompositeDataItem::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.

Definition at line 223 of file composite_data_item.inc.

224  {
225  foreach($this->__objects as $obj)
226  {
227  if ($obj->hasRelation($relation)) return true;
228  }
229 
230  return false;
231  }

◆ indexedQuery()

CompositeDataItem::indexedQuery (   $constraints = "",
  $indexBy = "" 
)

Performs a query against the database, returning an array of CompositeDataItem objects of the specified class, indexed by a particular field.

If more than one item is found that matches the same value of the indexing field, the results entry is promoted to an array automatically. However, in cases where this is common, using groupedQuery() might simplify your calling code.

Parameters
string$constraintsoptional constraint clause to apply to the query in the form "WHERE ... [ORDER BY ...]"
string$fieldoptional the name of the field by which to index the results (See InclusionFilter and ExclusionFilter)

Definition at line 765 of file composite_data_item.inc.

766  {
767  $field = "";
768  $idxClass = "";
769 
770  if ($indexBy != "")
771  {
772  list($idxClass, $field) = explode(".", $indexBy);
773  }
774  else
775  {
776  $idxClass = $this->class[0];
777  $obj = new $class;
778  $field = $obj->primary_key;
779  }
780 
781  $query = $this->generateQuery($constraints);
782 
783  try
784  {
786  $result = $db->prepare($query);
787  $result->execute();
788 
789  $items = array();
790 
791  while($line = $result->fetch())
792  {
793  $item = new JoinResult($this->tag);
794 
795  for($i = 0; $i < count($this->classes); ++$i)
796  {
797  $class = $this->classes[$i];
798  $item->$class = new $class; //Hack to work around PHP's stupid implementation of get_class()
799  $item->$class->populate($line);
800  }
801 
802  $idx = $item->$idxClass->$field;
803  if (array_key_exists($idx, $items))
804  {
805  // Implicitly promote to array if there is a collision
806  if (!is_array($items[$idx]))
807  {
808  $items[$idx] = array($items[$idx]);
809  }
810  $items[$idx][] = $item;
811  }
812  else
813  {
814  $items[$idx] = $item;
815  }
816  }
817 
818  unset($result);
819 
820  return $items;
821  }
822  catch(PDOException $e)
823  {
824  die("InnerJoin::indexedQuery() failed - ".$e->getMessage());
825  }
826  }
static getConnection()
Retrieves a reference to the global database connection.
JoinResult is an empty placeholder class.
Definition: join.inc:42

◆ insert()

CompositeDataItem::insert ( )

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

Definition at line 487 of file composite_data_item.inc.

488  {
489  // Insert and chain primary/foreign keys, from the inside out
490 
491  $num = count($this->__objects);
492 
493  for($i = $num - 1; $i >= 0; --$i)
494  {
495  $this->__objects[$i]->insert();
496  if ($i > 0)
497  {
498  $pk = $this->__objects[$i]->getPrimaryKey();
499  $this->__objects[$i - 1]->set($pk, $this->__objects[$i]->get($pk));
500  }
501  }
502  }

◆ joinTransaction()

CompositeDataItem::joinTransaction (   $tx)

Join the DataItem to the specified DataTransaction.

Parameters
DataTransaction$txthe transaction to join

Definition at line 91 of file composite_data_item.inc.

92  {
93  foreach($this->__objects as $obj)
94  {
95  $obj->joinTransaction($tx);
96  }
97 
98  $this->__tx = $tx;
99  }

◆ load()

CompositeDataItem::load (   $id)

Load the object with the specified primary key.

Parameters
intid the primary key value to instantiate from

Definition at line 386 of file composite_data_item.inc.

387  {
388  // Load from the outside in, chaining on the
389  // primary key/foreign key relationship at each level
390 
391  $num = count($this->__objects);
392 
393  for($i = 0; $i < $num; ++$i)
394  {
395  $this->__objects[$i]->load($id);
396 
397  if ($i == $num - 1) continue;
398 
399  $id = $this->__objects[$i]->get($this->__objects[$i+1]->getPrimaryKey());
400  }
401  }
getPrimaryKey()
Retrieves the primary key field name.

◆ loadFromBase()

CompositeDataItem::loadFromBase (   $id)

Definition at line 403 of file composite_data_item.inc.

404  {
405  $num = count($this->__objects);
406 
407  $this->__objects[$num - 1]->load($id);
408 
409  for($i = $num - 2; $i >= 0; --$i)
410  {
411  trace("BOOGER!", 3);
412  $pk = $this->__objects[$i+1]->getPrimaryKey();
413  $cl = get_class($this->__objects[$i]);
414  $obj = querySingle($cl, "WHERE $pk=".$this->__objects[$i+1]->get($pk));
415  $this->__objects[$i] = $obj;
416  $this->$cl = $obj;
417  }
418  }
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
querySingle($class)
Performs a query against the database and returns a matching singleton object.
Definition: data_item.inc:1711

◆ populate()

CompositeDataItem::populate (   $line)

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 117 of file composite_data_item.inc.

118  {
119  // Load all component objects from the outside in.
120 
121  $this->__objects[0]->populate($line);
122  $num = count($this->__objects);
123 
124  for($i = 1; $i < num; ++$i)
125  {
126  $pk = $this->__objects[$i];
127  $key = $obj->get($pk);
128  $obj->load($key);
129  }
130  }

◆ prettifyClassName()

CompositeDataItem::prettifyClassName (   $plural = false)

Definition at line 687 of file composite_data_item.inc.

688  {
689  $c = ($this->pretty_class_name) ? $this->pretty_class_name : get_class($this);
690  $c = preg_replace(array("/([a-z])([A-Z0-9])/",
691  "/_/"),
692  array("$1 $2",
693  " "),
694  $c);
695  $c = ucwords($c);
696 
697  if ($plural)
698  {
699  pluralize($c);
700  }
701 
702  return $c;
703  }
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1377

◆ prettifyFieldName()

CompositeDataItem::prettifyFieldName (   $field)

Definition at line 676 of file composite_data_item.inc.

677  {
678  foreach($this->__objects as $obj)
679  {
680  if ($obj->hasField($field)) return $obj->prettifyFieldName($field);
681  }
682 
683  return $field;
684  }

◆ query()

CompositeDataItem::query (   $constraints = "",
  $page = -1,
  $size = -1 
)

Definition at line 728 of file composite_data_item.inc.

729  {
730  $join = new InnerJoin();
731  foreach($this->__objects as $obj)
732  {
733  $join->add(get_class($obj));
734  }
735 
736  $joinResults = $join->query($constraints, $page, $size);
737 
738  $results = array();
739 
740  foreach($joinResults as $result)
741  {
742  $obj = clone($this);
743  foreach($obj->__objects as $target)
744  {
745  $cl = get_class($target);
746  $target->copy($result->$cl);
747  }
748 
749  $results[] = $obj;
750  }
751 
752  return $results;
753  }
This class is used to programmatically perform inner join queries across multiple objects...
Definition: join.inc:644

◆ relateTo()

CompositeDataItem::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.

Definition at line 713 of file composite_data_item.inc.

714  {
715  if ($field == "") $field = $target->getPrimaryKey();
716 
717  foreach($this->__objects as $obj)
718  {
719  if ($obj->hasField($field))
720  {
721  $obj->relateTo($target, $field);
722  return;
723  }
724  }
725  }

◆ save()

CompositeDataItem::save ( )

Store the object in the database.

Definition at line 423 of file composite_data_item.inc.

424  {
425  // Save from the inside out, chaining on the
426  // primary key/foreign key relationship at each level
427  $num = count($this->__objects);
428 
429  for($i = $num - 1; $i >= 0; --$i)
430  {
431  if ($this->__objects[$i]->hasField("composite_class"))
432  {
433  $this->__objects[$i]->set("composite_class", get_class($this));
434  }
435 
436  $this->__objects[$i]->save();
437  if ($i != 0)
438  {
439  $pk = $this->__objects[$i]->getPrimaryKey();
440  $this->__objects[$i - 1]->set($pk, $this->__objects[$i]->get($pk));
441  }
442  }
443  }
hasField($field)
Returns true if this DataItem contains a field with the specified name and that field is not excluded...

◆ select()

CompositeDataItem::select ( )

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

Definition at line 448 of file composite_data_item.inc.

449  {
450  // Select from the outside in, chaining on the
451  // primary key/foreign key relationship at each level
452 
453  $this->__objects[0]->select();
454 
455  $num = count($this->__objects);
456 
457  for($i = 1; $i < $num; $i)
458  {
459  $pk = $this->__objects[$i]->getPrimaryKey();
460  $this->__objects[$i]->set($pk, $this->__objects[$i - 1]->get($pk));
461  $this->__objects[$i]->select();
462  }
463  }

◆ set()

CompositeDataItem::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

Definition at line 294 of file composite_data_item.inc.

295  {
296  foreach($this->__objects as $obj)
297  {
298  if ($obj->hasField($field)) $obj->set($field, $value);
299  }
300  }

◆ setFilter()

CompositeDataItem::setFilter (   $filter)

Sets the filter on this object.

Parameters
$filterthe filter

Definition at line 329 of file composite_data_item.inc.

330  {
331  foreach($this->__objects as $obj)
332  {
333  $obj->setFilter($filter);
334  }
335  }

◆ toXML()

CompositeDataItem::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).

Definition at line 634 of file composite_data_item.inc.

635  {
636  $xml = str_repeat(" ", $indent) . "<" . get_class($this) . ">\n";
637  foreach ($this as $field => $val)
638  {
639  if (is_object($val))
640  {
641  $xml .= $val->toXML($indent + 1);
642  }
643  }
644 
645  $xml .= str_repeat(" ", $indent) . "</" . get_class($this) . ">\n";
646 
647  return $xml;
648  }

◆ update()

CompositeDataItem::update ( )

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

Definition at line 476 of file composite_data_item.inc.

477  {
478  foreach($this->__objects as $obj)
479  {
480  $obj->update();
481  }
482  }

Member Data Documentation

◆ $__objects

CompositeDataItem::$__objects

Definition at line 39 of file composite_data_item.inc.

◆ $__tx

CompositeDataItem::$__tx = null

Definition at line 40 of file composite_data_item.inc.


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