diff --git a/dblayeroo.php b/dblayeroo.php index 1331f6e..92c269c 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1336,7 +1336,8 @@ class dblayeroo $this->debugLog ("Entering setForeignObj (OBJECT)"); if (! is_object ($object)) $this->DBException ("Invalid setForeignObj parameter: not an object"); - if (! is_subclass_of ($object, __CLASS__)) + if (! is_subclass_of ($object, __CLASS__) && + get_class ($object) !== "dblayeroo") $this->DBException ( "Invalid object provided to setForeignObj (not dblayeroo object)"); if (! isset ($object->table)) @@ -1581,7 +1582,8 @@ class dblayeroo $this->DBException ("Invalid joinType provided to join (not known)"); if (! is_object ($object)) $this->DBException ("Invalid object provided to join (not object)"); - if (! is_subclass_of ($object, __CLASS__)) + if (! is_subclass_of ($object, __CLASS__) && + get_class ($object) !== "dblayeroo") $this->DBException ( "Invalid object provided to join (not dblayeroo object)"); if ($this->dsn !== $object->dsn) @@ -1628,18 +1630,6 @@ class dblayeroo // fields of object $this->joins[] = "$joinType JOIN ". $this->sep.$object->tableprefix.$object->table.$this->sep." ON $tmp"; - // Correct the WHERE in the main with the object WHERE - $this->whereExpression = array_merge ($object->whereExpression, - $this->whereExpression); - $this->whereValues = array_merge ($object->whereValues, $this->whereValues); - // Add the new object fields to the this. The fields must be available to - // be normalized at the end - $tmp = array (); - foreach ($object->fields as $key=>$data) - { - $tmp[$this->sep.$object->tableprefix.$object->table.$this->sep.".". - $this->sep.$key.$this->sep] = $data; - } return $this; } /* }}} */ @@ -1763,6 +1753,56 @@ class dblayeroo } /* }}} */ + /** Get the WHERE clause of the object. + * If the joinObject is set, return all the WHERE clauses + */ + public function whereGetExpression () + /* {{{ */ + { + $whereExpression = $this->whereExpression; + if ($whereExpression === null) + $whereExpression = array (); + if ($this->joinObject !== null) + { + foreach ($this->joinObject as $obj) + { + $exp = $obj->whereGetExpression (); + if (count ($whereExpression) && count ($exp)) + $whereExpression[] = "AND"; + if (count ($exp)) + $whereExpression[] = "("; + $whereExpression = array_merge ($whereExpression, + $exp); + if (count ($exp)) + $whereExpression[] = ")"; + } + } + return $whereExpression; + } + /* }}} */ + + /** Get the WHERE values of the object. + * If the joinObject is set, return all the WHERE clauses with AND and + * parenthesis + */ + public function whereGetValues () + /* {{{ */ + { + $whereValues = $this->whereValues; + if ($whereValues === null) + $whereValues = array (); + if ($this->joinObject !== null) + { + foreach ($this->joinObject as $obj) + { + $whereValues = array_merge ($whereValues, + $obj->whereGetValues ()); + } + } + return $whereValues; + } + /* }}} */ + /** Add a new ORDER sort. The multiple ORDERS are used from the first added to * the last added * @param string $field The field to sort @@ -1927,8 +1967,9 @@ class dblayeroo $sql .= " $displayColumns\n FROM $this->sep$this->tableprefix". "$this->table$this->sep"; $sql .= "\n ".$this->joinsGet (); - if (! empty ($this->whereExpression)) - $sql .= "\n WHERE ". implode (" ", $this->whereExpression); + $whereGetExpression = $this->whereGetExpression (); + if (! empty ($whereGetExpression)) + $sql .= "\n WHERE ". implode (" ", $whereGetExpression); if (count ($order)) $sql .= "\n ORDER BY ". implode (",", $order); if (! empty ($this->limitExpression)) @@ -1964,8 +2005,9 @@ class dblayeroo break; case "DELETE": $sql = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep"; - if (! empty ($this->whereExpression)) - $sql .= " WHERE ". implode (" ", $this->whereExpression); + $whereGetExpression = $this->whereGetExpression (); + if (! empty ($whereGetExpression)) + $sql .= "\n WHERE ". implode (" ", $whereGetExpression); if (! empty ($this->orderExpression)) $sql .= " ORDER BY ". implode (",", $this->orderExpression); if (! empty ($this->limitExpression)) @@ -1987,8 +2029,9 @@ class dblayeroo $sql .= $this->sep.$key.$this->sep."=:".$hash; $i++; } - if (! empty ($this->whereExpression)) - $sql .= " WHERE ". implode (" ", $this->whereExpression); + $whereGetExpression = $this->whereGetExpression (); + if (! empty ($whereGetExpression)) + $sql .= "\n WHERE ". implode (" ", $whereGetExpression); if (! empty ($this->orderExpression)) $sql .= " ORDER BY ". implode (",", $this->orderExpression); if (! empty ($this->limitExpression)) @@ -2014,7 +2057,7 @@ class dblayeroo $text = ""; if (!$textForm) $st = self::$instance[$this->dsn]->prepare ($sql); - foreach ($this->whereValues as $hash=>$val) + foreach ($this->whereGetValues () as $hash=>$val) { $field = $val["field"]; $value = $val["value"]; @@ -2364,7 +2407,7 @@ class dblayeroo } $this->debugLog ("Entering createRequest ()"); $sql = $this->createRequest (); - $this->debugLog ("Entering prepareRequest (",$sql,", ",false,")"); + $this->debugLog ("Entering prepareRequest (XXX, ",false,")"); $st = $this->prepareRequest ($sql, false); $this->debugLog ("'",$this->getDisplayQuery (),"'"); $st->execute ();