public function criteriaToQuery($criteria) { if ($criteria == null) { $criteria = new Criteria(); } $db = Database::connection(); // assemble a query string. // if exactQuery is specified - no problem. just run it. responsibility is on // the user ;-) if ($criteria->getExplicitQuery() != null) { return $criteria->getExplicitQuery(); } // ok - otherwise now we should counstruct the query $q = "SELECT "; if ($criteria->isDistinct()) { $q .= " DISTINCT "; } // check if the query has an explicit list of fields to selec if ($criteria->getExplicitFields() != null) { $q .= $criteria->getExplicitFields(); } else { $q .= $this->fieldListString(); } if ($criteria->getExplicitFrom() != null) { $q .= " FROM " . $criteria->getExplicitFrom(); } else { $q .= " FROM " . $this->tableName; } $whereString = $criteria->whereString(); if ($whereString != null) { $q .= " WHERE " . $criteria->whereString(); } $q .= " " . $criteria->groupByString(); $q .= " " . $criteria->orderString(); $q .= " " . $criteria->limitString(); if ($criteria->isForUpdate()) { $q .= " FOR UPDATE"; } return $q; }
public function criteriaToQuery($criteria) { if ($criteria == null) { $criteria = new Criteria(); } $db = Database::connection(); // assemble a query string. // if exactQuery is specified - no problem. just run it. responsibility is on // the user ;-) if ($criteria->getExplicitQuery() != null) { return $criteria->getExplicitQuery(); } // ok - otherwise now we should counstruct the query $joins = $criteria->getJoins(); $q = "SELECT "; if ($criteria->isDistinct()) { $q .= " DISTINCT "; if ($criteria->isDistinct() !== true) { $q .= " ON (" . $criteria->isDistinct() . ") "; } } // check if the query has an explicit list of fields to selec if ($criteria->getExplicitFields() != null) { $q .= $criteria->getExplicitFields(); } else { if ($joins == null) { $q .= ' ' . $this->tableName . '.* '; //$this->fieldListString(); } else { $q .= ' ' . $this->fieldListStringSpecialJoin(); foreach ($joins as $join) { // get aliased field list $peer = self::peerForTable($join['foreignTable']); $q .= ', ' . $peer->fieldListStringSpecialJoin(); } } } if ($criteria->getExplicitFrom() != null) { $q .= " FROM " . $criteria->getExplicitFrom(); } else { $q .= " FROM " . $this->tableName; if ($joins != null) { foreach ($joins as $join) { $q .= ', ' . $join['foreignTable']; } } } $whereString = $criteria->whereString(); if ($joins != null) { if ($whereString != null) { $q .= " WHERE (" . $whereString . ') AND'; } else { $q .= ' WHERE '; } $first = true; foreach ($joins as $join) { if ($first) { $first = false; } else { $q .= " AND "; } $q .= ' '; if (strpos($join['localKey'], '.') === false) { $q .= $this->tableName . '.'; } $q .= $join['localKey'] . ' = ' . $join['foreignTable'] . '.' . $join['foreignKey'] . ' '; } } elseif ($whereString != null) { $q .= " WHERE " . $whereString; } $q .= " " . $criteria->groupByString(); $q .= " " . $criteria->orderString(); $q .= " " . $criteria->limitString(); if ($criteria->isForUpdate()) { $q .= " FOR UPDATE"; } return $q; }