Author: Leon.van.der.Ree
Date: 2010-03-26 16:57:03 +0100 (Fri, 26 Mar 2010)
New Revision: 28805
Modified:
plugins/sfPropelObjectPathBehaviorPlugin/branches/1.5/lib/ObjectPathCriteria.php
Log:
fixed bug when joining deeper objectPaths
added support for propertyPaths when sorting and filtering
Modified:
plugins/sfPropelObjectPathBehaviorPlugin/branches/1.5/lib/ObjectPathCriteria.php
===================================================================
---
plugins/sfPropelObjectPathBehaviorPlugin/branches/1.5/lib/ObjectPathCriteria.php
2010-03-26 15:32:11 UTC (rev 28804)
+++
plugins/sfPropelObjectPathBehaviorPlugin/branches/1.5/lib/ObjectPathCriteria.php
2010-03-26 15:57:03 UTC (rev 28805)
@@ -14,7 +14,7 @@
* Recursively performs JoinWith on the provided ObjectPaths
*
* @param string $objectPath one or more strings with objectPaths
- * @return string
+ * @return ObjectPathCriteria fluid interface
*/
public function joinByObjectPath($objectPath)
{
@@ -36,8 +36,9 @@
// don't perform JoinWith, if already joined
// alternatively I could test if the join is not already in the
with-array
- // (this is a little more computational intense, but will perform with
even though the join has already been performed)
- if (!isset($this->joins[$alias]))
+ // (this is a little more computational intense, but will perform with
even though the join has already been performed)
+ $newJoin = !isset($this->joins[$alias]);
+ if ($newJoin)
{
$this->joinWith($relationName.' '.$alias);
}
@@ -45,9 +46,15 @@
// if more relations are provided, continue (recursively) parsing the
objectPath
if (isset($relationsNames[1]))
{
- $this->useQuery($alias)
- ->joinByObjectPath($relationsNames[1])
- ->endUse();
+ $related = $this->useQuery($alias)
+ ->joinByObjectPath($relationsNames[1]);
+
+ // merge down joined relation, if new
+ if ($newJoin)
+ {
+ $related->endUse();
+ }
+
}
}
@@ -123,7 +130,8 @@
$alias = $this->translateObjectPathToAlias($objectPath);
if (isset($this->joins[$alias])) {
- return
$this->useQuery($alias)->getColumnFromName($columnName, $failSilently);
+ return $this->useQuery($alias)
+ ->getColumnFromName($columnName,
$failSilently);
}
}
@@ -131,6 +139,80 @@
// only columnName provided, or relation-name is unknown (which
might be a table alias)
// process normal column as before
return parent::getColumnFromName($propertyPath, $failSilently);
- }
+ }
+
+ /**
+ * Adds a condition on a column based on a propertyPath and a value
+ * Uses introspection to translate the propertyPath into a fully qualified
name
+ * <code>
+ * $c->filterBy('Relation.Title', 'table_alias.foo');
+ * </code>
+ *
+ * @see Criteria::add()
+ *
+ * @param string $column A string representing thecolumn phpName,
e.g. 'AuthorId'
+ * @param mixed $value A value for the condition
+ * @param string $comparison What to use for the column comparison,
defaults to Criteria::EQUAL
+ *
+ * @return ModelCriteria The current object, for fluid interface
+ */
+ public function filterBy($column, $value, $comparison = Criteria::EQUAL)
+ {
+ // check if an objectPath has been given
+ $lastDot = strrpos($column, '.');
+ if ($lastDot !== false)
+ {
+ // get the objectPath
+ $objectPath = substr($column, 0, $lastDot);
+
+ // and get Related Query Class
+ $strRelated = $this->translateObjectPathToAlias($objectPath);
+ $column = $strRelated.'.'.substr($column, $lastDot + 1);
+
+ // TODO: this can be removed when filterBy is patched in propel
+ return $this->useQuery($strRelated)
+ ->filterBy($column, $value, $comparison)
+ ->endUse();;
+ }
+
+ return parent::filterBy($column, $value, $comparison);
+ }
+
+
+ /**
+ * Adds an ORDER BY clause to the query
+ * Usability layer on top of Criteria::addAscendingOrderByColumn() and
Criteria::addDescendingOrderByColumn()
+ * Infers $column and $order from $columnName and some optional arguments
+ * Examples:
+ * $c->orderBy('Book.CreatedAt')
+ * => $c->addAscendingOrderByColumn(BookPeer::CREATED_AT)
+ * $c->orderBy('Book.CategoryId', 'desc')
+ * => $c->addDescendingOrderByColumn(BookPeer::CATEGORY_ID)
+ *
+ * @param string $columnName The column to order by
+ * @param string $order The sorting order. Criteria::ASC by default,
also accepts Criteria::DESC
+ *
+ * @return ModelCriteria The current object, for fluid interface
+ */
+ public function orderBy($columnName, $order = Criteria::ASC)
+ {
+ // check if an objectPath has been given
+ $lastDot = strrpos($columnName, '.');
+ if ($lastDot !== false)
+ {
+ // get the objectPath
+ $objectPath = substr($columnName, 0, $lastDot);
+
+ // and get Related Query Class
+ $strRelated = $this->translateObjectPathToAlias($objectPath);
+ $columnName = $strRelated.'.'.substr($columnName, $lastDot + 1);
+
+// return $this->useQuery($strRelated)
+// ->orderBy($columnName, $order)
+// ->endUse();;
+ }
+
+ return parent::orderBy($columnName, $order);
+ }
}
\ No newline at end of file
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.