/** * Returns the "where" part of the query based on the criteria parameters * @param Criteria $criteria * @return String "where" part of the sql query */ private function GetWhereSQL($criteria) { $ands = $criteria->GetAnds(); $ors = $criteria->GetOrs(); // TODO: this all needs to move to the criteria object so it will recurse properly .... $where = $this->RemoveWherePrefix($criteria->GetWhere()); if (count($ands)) { $wdelim = $where ? " and " : ""; foreach ($ands as $c) { $tmp = $c->GetWhere(); $buff = $this->RemoveWherePrefix($tmp); if ($buff) { $where .= $wdelim . $buff; $wdelim = " and "; } } } if (count($ors)) { $where = trim($where) ? "(" . $where . ")" : ""; // no primary criteria. kinda strange $wdelim = $where ? " or " : ""; foreach ($ors as $c) { $tmp = $c->GetWhere(); $buff = $this->RemoveWherePrefix($tmp); if ($buff) { $where .= $wdelim . "(" . $buff . ")"; $wdelim = " or "; } } } // .. end of stuff that should be in criteria // prepend the "where" onto the statement if ($where) { $where = " where (" . trim($where) . ") "; } return $where; }
/** * Query for a child objects in a one-to-many relationship * * @access public * @param Phreezable $parent the parent object * @param string $keyname The name of the key representing the relationship * @return Criteria $criteria a Criteria object to limit the results */ public function GetOneToMany($parent, $keyname, $criteria) { // get the keymap for this child relationship $km = $this->GetKeyMap(get_class($parent), $keyname); // we need the value of the foreign key. (ex. to get all orders for a customer, we need Customer.Id) $parent_prop = $km->KeyProperty; $key_value = $parent->{$parent_prop}; if (!$criteria) { // if no criteria was specified, then create a generic one. we can specify SQL // code in the constructor, but we have to translate the properties into column names $foreign_table = $this->GetTableName($km->ForeignObject, $km->ForeignKeyProperty); $foreign_column = $this->GetColumnName($km->ForeignObject, $km->ForeignKeyProperty); $criteria = new Criteria("`" . $foreign_table . "`.`" . $foreign_column . "` = '" . $this->Escape($key_value) . "'"); } else { // ensure that the criteria passed in will filter correctly by foreign key $foreign_prop = $km->ForeignKeyProperty; // this is only for backwards compatibility with phreeze 2.0 apps if (self::$COMPAT_VERSION_2) { $criteria->{$foreign_prop} = $key_value; } // the current criteria "Equals" format "FieldName_Equals" $foreign_prop .= "_Equals"; $criteria->{$foreign_prop} = $key_value; // if this criteria has any or criterias attached, we need to set the foreign key to these // as well or else we'll get unexpected results foreach ($criteria->GetOrs() as $oc) { $oc->{$foreign_prop} = $key_value; } } return $this->Query($km->ForeignObject, $criteria); }