844 trace(print_r($this->constraints,
true), 3);
846 $tableConstraints = array();
847 $whereConstraints = array();
849 for($i = 0; $i <
count($this->classes); ++$i)
851 $class = $this->classes[$i];
852 $proto =& $this->protos[$class];
855 $this->aliases[$proto->table] = $alias;
857 if ($tables) $tables .=
", ";
858 $tables .=
"{$proto->table} $alias";
860 if (array_key_exists($class, $this->constraints))
862 $c = $this->constraints[$class];
864 foreach($proto->getFields() as $field => $type)
866 $c = preg_replace(
"/([\s\(])(".$field.
")\\b/",
"$1t$i.$2", $c);
869 $c =
"(".preg_replace(
"/^\\s*(WHERE\\s+|AND\\s+|OR\\s+)/i",
"", $c).
")";
874 $this->whereConstraints[$class] = $c;
879 $this->tableConstraints[$class] = $c;
883 $f = $proto->getFieldList($alias);
884 if ($fields && $f) $fields .=
", ";
888 for($i = 0; $i <
count($this->xrefs); ++$i)
890 $xref = $this->xrefs[$i];
893 $this->xrefAliases[$xref->table] = $alias;
895 if ($tables) $tables .=
", ";
896 $tables .=
"{$xref->table} $alias";
899 if ($this->unique) $fields =
"DISTINCT $fields";
901 $classConstraints = implode(
" AND ", array_values($this->whereConstraints));
905 if ($classConstraints) $classConstraints =
" AND ".$classConstraints;
906 $constraints =
"WHERE 1=1 $classConstraints $constraints";
910 if ($classConstraints) $classConstraints = $classConstraints.
" AND ";
915 $join = $this->protos[$this->classes[0]]->table .
" t0";
921 for($i = 0; $i <
count($this->classes) - 1; ++$i)
923 $c = $this->classes[$i];
924 $p = $this->protos[$c];
927 for ($j = $i+1; $j <
count($this->classes); ++$j)
929 $d = $this->classes[$j];
930 $q = $this->protos[$d];
934 trace($p->primary_key.
" ".array_key_exists($p->primary_key, $q->getFields()), 5);
935 trace($q->primary_key.
" ".array_key_exists($q->primary_key, $p->getFields()), 5);
937 if (array_key_exists($p->primary_key, $q->getFields()))
939 if ($this->excludedKeys[$d][$p->primary_key] ||
940 $this->excludedKeys[$c][$p->primary_key])
946 $idx =
"{$j}-{$p->primary_key}";
954 $join .=
" AND t$i.{$p->primary_key}=t$j.{$p->primary_key}";
958 $join .=
" LEFT OUTER JOIN {$q->table} t$j ON t$i.{$p->primary_key}=t$j.{$p->primary_key}";
959 if ($this->tableConstraints[$d])
961 $join .=
" AND ".$this->tableConstraints[$d];
966 $joined[$idx] =
true;
969 else if (array_key_exists($q->primary_key, $p->getFields()))
971 if ($this->excludedKeys[$d][$q->primary_key] ||
972 $this->excludedKeys[$c][$q->primary_key])
977 $idx =
"{$j}-{$q->primary_key}";
986 $join .=
" AND t$i.{$q->primary_key}=t$j.{$q->primary_key}";
990 $join .=
" LEFT OUTER JOIN {$q->table} t$j ON t$i.{$q->primary_key}=t$j.{$q->primary_key}";
994 $joined[$idx] =
true;
1003 for($i = 0; $i <
count($this->classes); ++$i)
1005 $c = $this->classes[$i];
1006 $p = $this->protos[$c];
1007 foreach($p->getFields() as $field => $type)
1012 foreach($p->calculatedFields as $field => $expr)
1014 foreach($p->getFields() as $f => $type)
1016 $expr = preg_replace(
"/([\s\(])(".$f.
")\\b/",
"$1t$i.$2", $expr);
1025 foreach($this->xrefs as $xref)
1027 $map = $this->xrefFieldMaps[$xref->table];
1029 foreach($this->protos as $p)
1031 $pk = $p->primary_key;
1032 $xk = ($map && array_key_exists($pk, $map)) ? $map[$pk] : $pk;
1034 if ($xref->hasField($xk))
1036 $xa = $this->xrefAliases[$xref->table];
1037 $ta = $this->aliases[$p->table];
1039 $join .=
" LEFT OUTER JOIN {$xref->table} $xa ON $xa.{$xk}=$ta.{$pk}";
1047 if (preg_match(
"/^(.*?)(ORDER\\s+BY.*)/i",
$constraints, $matches))
1050 $orderBy = $matches[2];
1052 else if (preg_match(
"/^(.*?)(LIMIT.*)/i",
$constraints, $matches))
1055 $orderBy = $matches[2];
1058 if ($countOnly) $fields = (is_bool($countOnly)) ?
"COUNT(1) as result" :
"COUNT($countOnly) as result";
1060 $query =
"SELECT $fields FROM $join $constraints $orderBy";
1062 trace(
"LeftOuterJoin::generateQuery() - $query", 3);
$constraints
Class-specific constraints add via the constrain() method.
count($constraints="", $countExpr=true)
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.