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

Public Member Functions

 __construct ($class, $constraints="", $indexBy="")
 
 indexBy ($indexBy)
 
 execute ()
 
- Public Member Functions inherited from AbstractQuery
 __construct ($class, $constraints="")
 
 constraints ($constraints)
 Sets the constraint clause for the Query. More...
 
 filter ($filter)
 Sets a filter to constrain the fields retrieved when the query is executed. More...
 
 params ($params)
 Sets the bound parameters array. More...
 
 bind ()
 Binds placeholders to parameter values. More...
 
 execute ()
 

Static Public Member Functions

static create ($class, $constraints="", $indexBy="")
 

Public Attributes

 $indexBy
 
- Public Attributes inherited from AbstractQuery
 $class
 
 $constraints
 
 $filter
 
 $params
 

Detailed Description

Definition at line 36 of file indexed_query.inc.

Constructor & Destructor Documentation

◆ __construct()

IndexedQuery::__construct (   $class,
  $constraints = "",
  $indexBy = "" 
)

Definition at line 40 of file indexed_query.inc.

41  {
42  parent::__construct($class, $constraints);
43  $this->indexBy = $indexBy;
44  }
indexBy($indexBy)

Member Function Documentation

◆ create()

static IndexedQuery::create (   $class,
  $constraints = "",
  $indexBy = "" 
)
static

Definition at line 124 of file indexed_query.inc.

◆ execute()

IndexedQuery::execute ( )

Definition at line 52 of file indexed_query.inc.

53  {
54  $prototype = new $this->class;
55 
56  if (!$this->indexBy) $this->indexBy = $prototype->getPrimaryKey();
57 
58  if ($this->filter)
59  {
60  $prototype->filter = $this->filter;
61  }
62 
63  $order_by_idx = strpos(strtoupper($this->constraints), "ORDER BY");
64  if ($order_by_idx !== false)
65  {
66  $orderBy = substr($this->constraints, $order_by_idx);
67 
68  $this->constraints = substr($this->constraints, 0, $order_by_idx);
69  }
70 
71  $fields = $prototype->getFieldList();
72 
73  if ($this->constraints == "") $this->constraints = "WHERE 1=1";
74  $query = "SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint()." $orderBy";
75  trace("DataItem indexedQuery(): $query", 3);
76 
77  $items = array();
78 
79  $field = $this->indexBy;
80 
81  $useFormat = preg_match("/^[A-Za-z0-9_]*$/", $field) ? false : true;
82 
83  try
84  {
86 
87  $result = $db->prepare($query);
88  $result->execute($this->params);
89 
90  while($line = $result->fetch())
91  {
92  $item = new $this->class;
93  $item->populate($line);
94 
95  $val = ($useFormat) ? $item->format($field) : $item->get($field);
96 
97  if (array_key_exists($val, $items))
98  {
99  // Implicitly promote to array if there is a collision
100  if (!is_array($items[$val]))
101  {
102  $items[$val] = array($items[$val]);
103  }
104  $items[$val][] = $item;
105  }
106  else
107  {
108  $items[$val] = $item;
109  }
110  }
111 
112  unset($result);
113  }
114  catch(PDOException $e)
115  {
116  $err = "indexedQuery() failed - " . $e->getMessage();
117  trace($err, 2);
118  throw new DataItemException($err);
119  }
120 
121  return $items;
122  }
params($params)
Sets the bound parameters array.
Definition: query.inc:78
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
constraints($constraints)
Sets the constraint clause for the Query.
Definition: query.inc:55
filter($filter)
Sets a filter to constrain the fields retrieved when the query is executed.
Definition: query.inc:66
static getConnection()
Retrieves a reference to the global database connection.
indexBy($indexBy)

◆ indexBy()

IndexedQuery::indexBy (   $indexBy)

Definition at line 46 of file indexed_query.inc.

47  {
48  $this->indexBy = $indexBy;
49  return $this;
50  }
indexBy($indexBy)

Member Data Documentation

◆ $indexBy

IndexedQuery::$indexBy

Definition at line 38 of file indexed_query.inc.


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