summaryrefslogtreecommitdiff
path: root/framework/Data
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data')
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php2
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php18
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php4
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php13
-rw-r--r--framework/Data/ActiveRecord/TActiveRecord.php30
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php13
-rw-r--r--framework/Data/Common/Sqlite/TSqliteMetaData.php5
-rw-r--r--framework/Data/DataGateway/TDataGatewayCommand.php13
-rw-r--r--framework/Data/DataGateway/TTableGateway.php27
9 files changed, 104 insertions, 21 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php b/framework/Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php
index 9374af64..02d0b0e3 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php
@@ -51,7 +51,7 @@ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
* }
* </code>
* The static <tt>$RELATIONS</tt> property of PlayerRecord defines that the
- * property <tt>$team</tt> belongs to (or is a) <tt>TeamRecord</tt>s.
+ * property <tt>$team</tt> belongs to a <tt>TeamRecord</tt>.
*
* The team object may be fetched as follows.
* <code>
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php b/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
index e83ca37f..65989639 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
@@ -158,13 +158,16 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
}
/**
- * @return TDbCommandBuilder
+ * @return TDataGatewayCommand
*/
protected function getCommandBuilder()
{
return $this->getSourceRecord()->getRecordGateway()->getCommand($this->getSourceRecord());
}
+ /**
+ * @return TDataGatewayCommand
+ */
protected function getForeignCommandBuilder()
{
$obj = $this->getContext()->getForeignRecordFinder();
@@ -314,13 +317,16 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
return $success;
}
+ /**
+ * @return TDbCommandBuilder
+ */
protected function getAssociationTableCommandBuilder()
{
$conn = $this->getContext()->getSourceRecord()->getDbConnection();
return $this->getAssociationTable()->createCommandBuilder($conn);
}
- protected function hasAssociationData($builder,$data)
+ private function hasAssociationData($builder,$data)
{
$condition=array();
$table = $this->getAssociationTable();
@@ -331,13 +337,13 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
return intval($result) > 0;
}
- protected function addAssociationData($builder,$data)
+ private function addAssociationData($builder,$data)
{
$command = $builder->createInsertCommand($data);
return $this->getCommandBuilder()->onExecuteCommand($command, $command->execute()) > 0;
}
- protected function updateAssociationTable($obj,$fkObjects, $builder)
+ private function updateAssociationTable($obj,$fkObjects, $builder)
{
$source = $this->getSourceRecordValues($obj);
$foreignKeys = $this->findForeignKeys($this->getAssociationTable(), $fkObjects[0]);
@@ -351,7 +357,7 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
return $success;
}
- protected function getSourceRecordValues($obj)
+ private function getSourceRecordValues($obj)
{
$sourceKeys = $this->findForeignKeys($this->getAssociationTable(), $obj);
$indexValues = $this->getIndexValues(array_values($sourceKeys), $obj);
@@ -362,7 +368,7 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
return $data;
}
- protected function getForeignObjectValues($foreignKeys,$fkObject)
+ private function getForeignObjectValues($foreignKeys,$fkObject)
{
$data=array();
foreach($foreignKeys as $name=>$fKey)
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
index 4dc9743f..60afef89 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
@@ -191,10 +191,6 @@ abstract class TActiveRecordRelation
$prop = $this->getContext()->getProperty();
$source->{$prop} = isset($collections[$hash]) ? $collections[$hash] : array();
}
-
- public function updateAssociatedRecords()
- {
- }
}
?> \ No newline at end of file
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
index 12f2a874..16b29861 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
@@ -182,7 +182,6 @@ class TActiveRecordRelationContext
*/
public function updateAssociatedRecords($updateBelongsTo=false)
{
- Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelationCommand');
$success=true;
foreach($this->getRecordRelationships() as $property=>$relation)
{
@@ -190,7 +189,7 @@ class TActiveRecordRelationContext
if(($updateBelongsTo && $belongsTo) || (!$updateBelongsTo && !$belongsTo))
{
$obj = $this->getSourceRecord();
- if(!empty($obj->{$property}))
+ if(!$this->isEmptyFkObject($obj->{$property}))
{
$context = new self($this->getSourceRecord(),$property);
$success = $context->getRelationHandler()->updateAssociatedRecords() && $success;
@@ -199,6 +198,16 @@ class TActiveRecordRelationContext
}
return $success;
}
+
+ protected function isEmptyFkObject($obj)
+ {
+ if(is_object($obj))
+ return $obj instanceof TList ? $obj->count() === 0 : false;
+ else if(is_array($obj))
+ return count($obj)===0;
+ else
+ return empty($obj);
+ }
}
?> \ No newline at end of file
diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php
index 1f482e6a..a66ab21c 100644
--- a/framework/Data/ActiveRecord/TActiveRecord.php
+++ b/framework/Data/ActiveRecord/TActiveRecord.php
@@ -289,7 +289,15 @@ abstract class TActiveRecord extends TComponent
return $this->getRecordGateway()->deleteRecordsByPk($this,(array)$keys);
}
-
+ /**
+ * Alias for deleteByPk()
+ */
+ public function deleteAllByPks($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->deleteByPk($keys);
+ }
/**
* Delete multiple records using a criteria.
* @param string|TActiveRecordCriteria SQL condition or criteria object.
@@ -434,12 +442,28 @@ abstract class TActiveRecord extends TComponent
* class.
* @param string select SQL
* @param array $parameters
- * @return array matching active records.
+ * @return TActiveRecord
*/
public function findBySql($sql,$parameters=array())
{
$args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
$criteria = $this->getCriteria($sql,$parameters, $args);
+ $data = $this->getRecordGateway()->findRecordBySql($this,$criteria);
+ return $this->populateObject(get_class($this), $data);
+ }
+
+ /**
+ * Find records using full SQL, returns corresponding record object.
+ * The names of the column retrieved must be defined in your Active Record
+ * class.
+ * @param string select SQL
+ * @param array $parameters
+ * @return array matching active records.
+ */
+ public function findAllBySql($sql,$parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($sql,$parameters, $args);
$result = $this->getRecordGateway()->findRecordsBySql($this,$criteria);
return $this->collectObjects($result);
}
@@ -533,6 +557,8 @@ abstract class TActiveRecord extends TComponent
$condition = $method[9]==='_' ? substr($method,10) : substr($method,9);
else if($delete = substr(strtolower($method),0,8)==='deleteby')
$condition = $method[8]==='_' ? substr($method,9) : substr($method,8);
+ else if($delete = substr(strtolower($method),0,11)==='deleteallby')
+ $condition = $method[11]==='_' ? substr($method,12) : substr($method,11);
else
return null;//throw new TActiveRecordException('ar_invalid_finder_method',$method);
diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php
index d97844f7..80ce40c0 100644
--- a/framework/Data/ActiveRecord/TActiveRecordGateway.php
+++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php
@@ -214,11 +214,22 @@ class TActiveRecordGateway extends TComponent
* Return record data from sql query.
* @param TActiveRecord active record finder instance.
* @param TActiveRecordCriteria sql query
+ * @return array result.
+ */
+ public function findRecordBySql(TActiveRecord $record, $criteria)
+ {
+ return $this->getCommand($record)->findBySql($criteria);
+ }
+
+ /**
+ * Return record data from sql query.
+ * @param TActiveRecord active record finder instance.
+ * @param TActiveRecordCriteria sql query
* @return TDbDataReader result iterator.
*/
public function findRecordsBySql(TActiveRecord $record, $criteria)
{
- return $this->getCommand($record)->findBySql($criteria);
+ return $this->getCommand($record)->findAllBySql($criteria);
}
public function findRecordsByIndex(TActiveRecord $record, $criteria, $fields, $values)
diff --git a/framework/Data/Common/Sqlite/TSqliteMetaData.php b/framework/Data/Common/Sqlite/TSqliteMetaData.php
index 1a72622e..5e1c5f77 100644
--- a/framework/Data/Common/Sqlite/TSqliteMetaData.php
+++ b/framework/Data/Common/Sqlite/TSqliteMetaData.php
@@ -164,9 +164,8 @@ CREATE TABLE foo
CREATE TABLE bar
(
id INTEGER NOT NULL PRIMARY KEY,
- foo_id INTEGER NOT NULL CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE,
- foo_id2 CHAR(2) CONSTRAINT fk_foo_id REFERENCES foo(id2) ON DELETE CASCADE,
- bar_id INTEGER NOT NULL CONSTRAINT fk_bar_id REFERENCES bar(id)
+ foo_id INTEGER
+ CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE
);
*/
diff --git a/framework/Data/DataGateway/TDataGatewayCommand.php b/framework/Data/DataGateway/TDataGatewayCommand.php
index 80856a0e..60db80d6 100644
--- a/framework/Data/DataGateway/TDataGatewayCommand.php
+++ b/framework/Data/DataGateway/TDataGatewayCommand.php
@@ -280,13 +280,24 @@ class TDataGatewayCommand extends TComponent
}
/**
- * Find one or more matching records for arbituary SQL.
+ * Find one matching records for arbituary SQL.
* @param TSqlCriteria $criteria
* @return TDbDataReader record reader.
*/
public function findBySql($criteria)
{
$command = $this->getSqlCommand($criteria);
+ return $this->onExecuteCommand($command, $command->queryRow());
+ }
+
+ /**
+ * Find zero or more matching records for arbituary SQL.
+ * @param TSqlCriteria $criteria
+ * @return TDbDataReader record reader.
+ */
+ public function findAllBySql($criteria)
+ {
+ $command = $this->getSqlCommand($criteria);
return $this->onExecuteCommand($command, $command->query());
}
diff --git a/framework/Data/DataGateway/TTableGateway.php b/framework/Data/DataGateway/TTableGateway.php
index f278eed5..e389a98e 100644
--- a/framework/Data/DataGateway/TTableGateway.php
+++ b/framework/Data/DataGateway/TTableGateway.php
@@ -175,7 +175,7 @@ class TTableGateway extends TComponent
* Execute arbituary sql command with binding parameters.
* @param string SQL query string.
* @param array binding parameters, positional or named.
- * @return TDbDataReader query results.
+ * @return array query results.
*/
public function findBySql($sql, $parameters=array())
{
@@ -185,6 +185,19 @@ class TTableGateway extends TComponent
}
/**
+ * Execute arbituary sql command with binding parameters.
+ * @param string SQL query string.
+ * @param array binding parameters, positional or named.
+ * @return TDbDataReader query results.
+ */
+ public function findAllBySql($sql, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($sql,$parameters, $args);
+ return $this->getCommand()->findAllBySql($criteria);
+ }
+
+ /**
* Find one single record that matches the criteria.
*
* Usage:
@@ -314,6 +327,16 @@ class TTableGateway extends TComponent
}
/**
+ * Alias for deleteByPk()
+ */
+ public function deleteAllByPks($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->deleteByPk($keys);
+ }
+
+ /**
* Find the number of records.
* @param string|TSqlCriteria SQL condition or criteria object.
* @param mixed parameter values.
@@ -428,6 +451,8 @@ class TTableGateway extends TComponent
$condition = $method[9]==='_' ? substr($method,10) : substr($method,9);
else if($delete = substr(strtolower($method),0,8)==='deleteby')
$condition = $method[8]==='_' ? substr($method,9) : substr($method,8);
+ else if($delete = substr(strtolower($method),0,11)==='deleteallby')
+ $condition = $method[11]==='_' ? substr($method,12) : substr($method,11);
else
return null;