summaryrefslogtreecommitdiff
path: root/framework/Data
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data')
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php36
-rw-r--r--framework/Data/Common/TDbCommandBuilder.php21
-rw-r--r--framework/Data/DataGateway/TSqlCriteria.php28
3 files changed, 62 insertions, 23 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
index 28d80683..ff6d9554 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
@@ -69,14 +69,6 @@ class TActiveRecordRelationContext
}
/**
- * @return string foreign record class name.
- */
- public function getForeignRecordClass()
- {
- return $this->_relation[1];
- }
-
- /**
* @return array foreign key of this relations, the keys is dependent on the
* relationship type.
* @since 3.1.2
@@ -97,6 +89,14 @@ class TActiveRecordRelationContext
}
/**
+ * @return string foreign record class name.
+ */
+ public function getForeignRecordClass()
+ {
+ return $this->_relation[1];
+ }
+
+ /**
* @return string foreign key field names, comma delimited.
* @since 3.1.2
*/
@@ -106,6 +106,24 @@ class TActiveRecordRelationContext
}
/**
+ * @return string the query condition for the relation as specified in RELATIONS
+ * @since 3.1.2
+ */
+ public function getCondition()
+ {
+ return isset($this->_relation[3])?$this->_relation[3]:null;
+ }
+
+ /**
+ * @return array the query parameters for the relation as specified in RELATIONS
+ * @since 3.1.2
+ */
+ public function getParameters()
+ {
+ return isset($this->_relation[4])?$this->_relation[4]:array();
+ }
+
+ /**
* @return boolean true if the 3rd element of an TActiveRecord::$RELATION entry is set.
* @since 3.1.2
*/
@@ -156,7 +174,7 @@ class TActiveRecordRelationContext
$this->_property, get_class($this->_record), 'RELATIONS');
}
if($criteria===null)
- $criteria = new TActiveRecordCriteria;
+ $criteria = new TActiveRecordCriteria($this->getCondition(), $this->getParameters());
switch($this->getRelationType())
{
case TActiveRecord::HAS_MANY:
diff --git a/framework/Data/Common/TDbCommandBuilder.php b/framework/Data/Common/TDbCommandBuilder.php
index 0029b4c3..f358036c 100644
--- a/framework/Data/Common/TDbCommandBuilder.php
+++ b/framework/Data/Common/TDbCommandBuilder.php
@@ -159,10 +159,10 @@ class TDbCommandBuilder extends TComponent
*/
public function createFindCommand($where='1=1', $parameters=array(), $ordering=array(), $limit=-1, $offset=-1)
{
- if($where===null)
- $where='1=1';
$table = $this->getTableInfo()->getTableFullName();
- $sql = "SELECT * FROM {$table} WHERE {$where}";
+ $sql = "SELECT * FROM {$table}";
+ if(!empty($where))
+ $sql .= " WHERE {$where}";
return $this->applyCriterias($sql, $parameters, $ordering, $limit, $offset);
}
@@ -185,10 +185,10 @@ class TDbCommandBuilder extends TComponent
*/
public function createCountCommand($where='1=1', $parameters=array(),$ordering=array(), $limit=-1, $offset=-1)
{
- if($where===null)
- $where='1=1';
$table = $this->getTableInfo()->getTableFullName();
- $sql = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
+ $sql = "SELECT COUNT(*) FROM {$table}";
+ if(!empty($where))
+ $sql .= " WHERE {$where}";
return $this->applyCriterias($sql, $parameters, $ordering, $limit, $offset);
}
@@ -204,8 +204,8 @@ class TDbCommandBuilder extends TComponent
{
$table = $this->getTableInfo()->getTableFullName();
if (!empty($where))
- $where = 'WHERE '.$where;
- $command = $this->createCommand("DELETE FROM {$table} ".$where);
+ $where = ' WHERE '.$where;
+ $command = $this->createCommand("DELETE FROM {$table}".$where);
$this->bindArrayValues($command, $parameters);
return $command;
}
@@ -242,7 +242,10 @@ class TDbCommandBuilder extends TComponent
$fields = implode(', ', $this->getColumnBindings($data, true));
else
$fields = implode(', ', $this->getColumnBindings($data));
- $command = $this->createCommand("UPDATE {$table} SET {$fields} WHERE {$where}");
+
+ if (!empty($where))
+ $where = ' WHERE '.$where;
+ $command = $this->createCommand("UPDATE {$table} SET {$fields}".$where);
$this->bindArrayValues($command, array_merge($data, $parameters));
return $command;
}
diff --git a/framework/Data/DataGateway/TSqlCriteria.php b/framework/Data/DataGateway/TSqlCriteria.php
index d073cd10..a17ac0c6 100644
--- a/framework/Data/DataGateway/TSqlCriteria.php
+++ b/framework/Data/DataGateway/TSqlCriteria.php
@@ -51,6 +51,7 @@ class TSqlCriteria extends TComponent
$this->_parameters->copyFrom((array)$parameters);
$this->_ordersBy=new TAttributeCollection;
$this->_ordersBy->setCaseSensitive(true);
+
$this->setCondition($condition);
}
@@ -68,7 +69,14 @@ class TSqlCriteria extends TComponent
*/
public function setCondition($value)
{
- $this->_condition=$value;
+ if(!empty($value) && preg_match('/ORDER\s+BY\s+(.*?)$/i',$value,$matches)>0)
+ {
+ // condition contains ORDER BY, we need to strip it output
+ $this->_condition=substr($value,0,strpos($value,$matches[0]));
+ $this->setOrdersBy($matches[1]);
+ }
+ else
+ $this->_condition=$value;
}
/**
@@ -107,13 +115,23 @@ class TSqlCriteria extends TComponent
}
/**
- * @param ArrayAccess ordering clause.
+ * @param mixed ordering clause.
*/
public function setOrdersBy($value)
{
- if(!(is_array($value) || $value instanceof ArrayAccess))
- throw new TException('value must be array or ArrayAccess');
- $this->_ordersBy->copyFrom($value);
+ if(is_array($value) || $value instanceof Traversable)
+ $this->_ordersBy->copyFrom($value);
+ else
+ {
+ $value=trim(preg_replace('/\s+/',' ',(string)$value));
+ $orderBys=array();
+ foreach(explode(',',$value) as $orderBy)
+ {
+ $vs=explode(' ',trim($orderBy));
+ $orderBys[$vs[0]]=isset($vs[1])?$vs[1]:'asc';
+ }
+ $this->_ordersBy->copyFrom($orderBys);
+ }
}
/**