[limb-svn] r5997 - in 3.x/trunk/limb/active_record/src: . toolkit
svn at limb-project.com
svn at limb-project.com
Mon Jun 18 16:27:21 MSD 2007
Author: pachanga
Date: 2007-06-18 16:27:21 +0400 (Mon, 18 Jun 2007)
New Revision: 5997
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5997
Modified:
3.x/trunk/limb/active_record/src/lmbARManyToManyCollection.class.php
3.x/trunk/limb/active_record/src/lmbARMetaInfo.class.php
3.x/trunk/limb/active_record/src/lmbAROneToManyCollection.class.php
3.x/trunk/limb/active_record/src/lmbARRecordSetDecorator.class.php
3.x/trunk/limb/active_record/src/lmbARRelationCollection.class.php
3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
3.x/trunk/limb/active_record/src/toolkit/lmbARTools.class.php
Log:
-- experimental commit - enabling lmbActiveRecord instances to bind to the specific db connection(AR-11)
* lmbActiveRecord constructor now accepts second optional argument with db connection object
* lmbActiveRecord :: $_db_conn stores instance db connection object
* static lmbActiveRecord :: setDefaultConnection($conn) sets shared db connection object which is used if no connection was passed to the constructor
* all static functions now accept optional argument with db connection object
* db connection object is propagated to all levels: to relation collections, decorators, table gateways, etc.
-- lmbActiveRecord :: decorateRecordSet() split into 2 methods: old decorateRecordSet(..) which is left as is and new instance method _decorateRecordSet()
Modified: 3.x/trunk/limb/active_record/src/lmbARManyToManyCollection.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARManyToManyCollection.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbARManyToManyCollection.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,19 +1,19 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
-lmb_require('limb/active_record/src/lmbARRelationCollection.class.php');
-
+lmb_require('limb/active_record/src/lmbARRelationCollection.class.php');
+
/**
* class lmbARManyToManyCollection.
*
* @package active_record
* @version $Id$
- */
+ */
class lmbARManyToManyCollection extends lmbARRelationCollection
{
protected function _createDbRecordSet2($criteria = null)
@@ -30,7 +30,7 @@
WHERE {$table}.id={$join_table}.$foreign_field AND
{$join_table}.{$field}=" . $this->owner->getId() . ' %where%';
- $query = new lmbSelectQuery($sql, lmbToolkit :: instance()->getDefaultDbConnection());
+ $query = new lmbSelectQuery($sql, $this->conn);
if($criteria)
$query->addCriteria($criteria);
return $query->getRecordSet();
@@ -38,21 +38,19 @@
protected function _createDbRecordSet($criteria = null)
{
- $conn = lmbToolkit :: instance()->getDefaultDbConnection();
-
$class = $this->relation_info['class'];
$object = new $class();
- $table = $conn->quoteIdentifier($object->getTableName());
+ $table = $this->conn->quoteIdentifier($object->getTableName());
- $join_table = $conn->quoteIdentifier($this->relation_info['table']);
- $field = $conn->quoteIdentifier($this->relation_info['field']);
- $foreign_field = $conn->quoteIdentifier($this->relation_info['foreign_field']);
+ $join_table = $this->conn->quoteIdentifier($this->relation_info['table']);
+ $field = $this->conn->quoteIdentifier($this->relation_info['field']);
+ $foreign_field = $this->conn->quoteIdentifier($this->relation_info['foreign_field']);
$sql = "SELECT $table.* FROM $table, $join_table
WHERE $table.id=$join_table.$foreign_field AND
$join_table.$field=" . $this->owner->getId() . ' %where%';
- $query = new lmbSelectQuery($sql, $conn);
+ $query = new lmbSelectQuery($sql, $this->conn);
if($criteria)
$query->addCriteria($criteria);
return $query->getRecordSet();
@@ -67,13 +65,13 @@
protected function _removeRelatedRecords()
{
- $table = new lmbTableGateway($this->relation_info['table']);
+ $table = new lmbTableGateway($this->relation_info['table'], $this->conn);
$table->delete(new lmbSQLFieldCriteria($this->relation_info['field'], $this->owner->getId()));
}
protected function _saveObject($object, $error_list = null)
{
- $table = new lmbTableGateway($this->relation_info['table']);
+ $table = new lmbTableGateway($this->relation_info['table'], $this->conn);
$object->save($error_list);
$table->insert(array($this->relation_info['field'] => $this->owner->getId(),
$this->relation_info['foreign_field'] => $object->getId()));
Modified: 3.x/trunk/limb/active_record/src/lmbARMetaInfo.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARMetaInfo.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbARMetaInfo.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,18 +1,18 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
- */
-
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
/**
* class lmbARMetaInfo.
*
* @package active_record
* @version $Id$
- */
+ */
class lmbARMetaInfo
{
protected $db_table = null;
@@ -21,12 +21,12 @@
protected $set_method_fields = array();
protected $cast_methods = array();
- function __construct($active_record)
+ function __construct($active_record, $conn = null)
{
if(!$table_name = $active_record->getTableName())
$table_name = lmb_under_scores(get_class($active_record));
- $this->db_table = lmbToolkit :: instance()->createTableGateway($table_name);
+ $this->db_table = lmbToolkit :: instance()->createTableGateway($table_name, $conn);
$this->db_column_names = $this->db_table->getColumnNames();
}
Modified: 3.x/trunk/limb/active_record/src/lmbAROneToManyCollection.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbAROneToManyCollection.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbAROneToManyCollection.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,25 +1,25 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
-lmb_require('limb/active_record/src/lmbARRelationCollection.class.php');
-
+lmb_require('limb/active_record/src/lmbARRelationCollection.class.php');
+
/**
* class lmbAROneToManyCollection.
*
* @package active_record
* @version $Id$
- */
+ */
class lmbAROneToManyCollection extends lmbARRelationCollection
{
protected function _createDbRecordSet($extra_criteria = null)
{
$class = $this->relation_info['class'];
- $object = new $class();
+ $object = new $class(null, $this->conn);
$criteria = new lmbSQLFieldCriteria($this->relation_info['field'], $this->owner->getId());
if($extra_criteria)
@@ -60,7 +60,8 @@
protected function _removeRelatedRecords()
{
lmbActiveRecord :: delete($this->relation_info['class'],
- new lmbSQLFieldCriteria($this->relation_info['field'], $this->owner->getId()));
+ new lmbSQLFieldCriteria($this->relation_info['field'], $this->owner->getId()),
+ $this->conn);
}
protected function _saveObject($object, $error_list = null)
Modified: 3.x/trunk/limb/active_record/src/lmbARRecordSetDecorator.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARRecordSetDecorator.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbARRecordSetDecorator.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,27 +1,29 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require('limb/core/src/lmbCollectionDecorator.class.php');
-lmb_require('limb/core/src/lmbClassPath.class.php');
-
+lmb_require('limb/core/src/lmbClassPath.class.php');
+
/**
* class lmbARRecordSetDecorator.
*
* @package active_record
* @version $Id$
- */
+ */
class lmbARRecordSetDecorator extends lmbCollectionDecorator
{
protected $class_path;
+ protected $conn;
- function __construct($record_set, $class_path = '')
+ function __construct($record_set, $class_path = '', $conn = null)
{
$this->class_path = $class_path;
+ $this->conn = $conn;
parent :: __construct($record_set);
}
@@ -56,10 +58,10 @@
$class = end(lmbActiveRecord :: decodeInheritancePath($path));
if(!class_exists($class))
throw new lmbException("Class '$class' not found");
- return new $class;
+ return new $class(null, $this->conn);
}
else
- return lmbClassPath :: create($this->class_path);
+ return lmbClassPath :: create($this->class_path, array(null, $this->conn));
}
function at($pos)
Modified: 3.x/trunk/limb/active_record/src/lmbARRelationCollection.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARRelationCollection.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbARRelationCollection.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,21 +1,21 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require('limb/core/src/lmbCollectionInterface.interface.php');
lmb_require('limb/core/src/lmbCollection.class.php');
-lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
-
+lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
+
/**
* abstract class lmbARRelationCollection.
*
* @package active_record
* @version $Id$
- */
+ */
abstract class lmbARRelationCollection implements lmbCollectionInterface
{
protected $relation;
@@ -23,16 +23,22 @@
protected $owner;
protected $dataset;
protected $criteria;
+ protected $conn;
protected $is_owner_new;
protected $decorators = array();
- function __construct($relation, $owner, $criteria = null)
+ function __construct($relation, $owner, $criteria = null, $conn = null)
{
$this->relation = $relation;
$this->owner = $owner;
$this->relation_info = $owner->getRelationInfo($relation);
$this->criteria = lmbSQLCriteria :: objectify($criteria);
+ if(is_object($conn))
+ $this->conn = $conn;
+ else
+ $this->conn = lmbToolkit :: instance()->getDefaultDbConnection();
+
$this->reset();
}
@@ -109,7 +115,7 @@
$rs = $this->_createDbRecordSet($criteria);
$this->_applySortParams($rs, $sort_params);
- $dataset = $object->decorateRecordSet($rs);
+ $dataset = $object->_decorateRecordSet($rs);
return $this->_applyDecorators($dataset);
}
Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -12,6 +12,7 @@
lmb_require('limb/dbal/src/lmbTableGateway.class.php');
lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
lmb_require('limb/dbal/src/drivers/lmbDbTypeInfo.class.php');
+lmb_require('limb/dbal/toolkit.inc.php');
lmb_require('limb/validation/src/lmbValidator.class.php');
lmb_require('limb/validation/src/lmbErrorList.class.php');
lmb_require('limb/validation/src/exception/lmbValidationException.class.php');
@@ -26,7 +27,7 @@
*
* @version $Id$
* @package active_record
- */
+ */
class lmbActiveRecord extends lmbObject
{
/**
@@ -46,6 +47,16 @@
*/
protected static $_global_listeners = array();
/**
+ * @var object database connection which is shared by all lmbActiveRecord instances
+ * if no connection passed explicitly into constructor
+ */
+ protected static $_default_db_conn;
+ /**
+ * @var object current object's database connection
+ * @see lmbDbConnection
+ */
+ protected $_db_conn;
+ /**
* @var object lmbTableGateway instance used to access underlying db table
*/
protected $_db_table;
@@ -157,14 +168,19 @@
* </code>
* @param array|integer Depending on argument type the new object is filled with properties or loaded from database
*/
- function __construct($magic_params = null)
+ function __construct($magic_params = null, $conn = null)
{
parent :: __construct();
$this->_defineRelations();
- $this->_db_meta_info = lmbToolkit :: instance()->getActiveRecordMetaInfo($this);
+ if(is_object($conn))
+ $this->_db_conn = $conn;
+ else
+ $this->_db_conn = self :: getDefaultConnection();
+ $this->_db_meta_info = lmbToolkit :: instance()->getActiveRecordMetaInfo($this, $this->_db_conn);
+
$this->_db_table = $this->_db_meta_info->getDbTable();
$this->_db_table_name = $this->_db_table->getTableName();
$this->_error_list = new lmbErrorList();
@@ -174,17 +190,38 @@
elseif(is_array($magic_params) || is_object($magic_params))
$this->import($magic_params);
}
-
/**
* Sets database resource identifier used for database access
* @param string DSN, e.g. mysql://root:secret@localhost/mydb
*/
static function setDefaultDSN($dsn)
{
- lmb_require('limb/dbal/toolkit.inc.php');
- lmbToolkit :: instance()->setDefaultDbDSN($dsn);
+ self :: $_default_db_conn = lmbToolkit :: instance()->createDbConnection($dsn);
}
/**
+ * Sets default database connection object
+ * @param object instance of concrete lmbDbConnection interface implementation
+ * @return object previous connection object
+ * @see lmbDbConnection
+ */
+ static function setDefaultConnection($conn)
+ {
+ $prev = self :: $_default_db_conn;
+ self :: $_default_db_conn = $conn;
+ return $prev;
+ }
+ /**
+ * Returns current default database connection object
+ * @return object instance of concrete lmbDbConnection interface implementation
+ * @see lmbDbConnection
+ */
+ static function getDefaultConnection()
+ {
+ if(is_object(self :: $_default_db_conn))
+ return self :: $_default_db_conn;
+ return lmbToolkit :: instance()->getDefaultDbConnection();
+ }
+ /**
* Returns current single table inheritance column name
* @return string
*/
@@ -225,9 +262,7 @@
return $this->_error_list;
}
- protected function _defineRelations()
- {
- }
+ protected function _defineRelations(){}
protected function _hasOne($relation_name, $info)
{
@@ -530,9 +565,9 @@
if(isset($info['collection']))
return new $info['collection']($relation, $this, $criteria);
elseif($this->_hasOneToManyRelation($relation))
- return new lmbAROneToManyCollection($relation, $this, $criteria);
+ return new lmbAROneToManyCollection($relation, $this, $criteria, $this->_db_conn);
else if($this->_hasManyToManyRelation($relation))
- return new lmbARManyToManyCollection($relation, $this, $criteria);
+ return new lmbARManyToManyCollection($relation, $this, $criteria, $this->_db_conn);
}
protected function _hasCollectionRelation($relation)
@@ -704,8 +739,9 @@
protected function _loadBelongsToObject($property)
{
- return lmbActiveRecord :: findFirst($this->_belongs_to[$property]['class'],
- array('criteria' => $this->_belongs_to[$property]['field'] . ' = ' . (int)$this->getId()));
+ return self :: findFirst($this->_belongs_to[$property]['class'],
+ array('criteria' => $this->_belongs_to[$property]['field'] . ' = ' . (int)$this->getId()),
+ $this->_db_conn);
}
protected function _loadManyBelongsToObject($property)
@@ -714,8 +750,9 @@
if(!$value && $this->_canManyBelongsToObjectBeNull($property))
return null;
- return lmbActiveRecord :: findById($this->_many_belongs_to[$property]['class'],
- $this->get($this->_many_belongs_to[$property]['field']));
+ return self :: findById($this->_many_belongs_to[$property]['class'],
+ $this->get($this->_many_belongs_to[$property]['field']),
+ $this->_db_conn);
}
protected function _loadOneToOneObject($property)
@@ -724,8 +761,9 @@
if(!$value && $this->_canHasOneObjectBeNull($property))
return null;
- return lmbActiveRecord :: findById($this->_has_one[$property]['class'],
- $this->get($this->_has_one[$property]['field']));
+ return self :: findById($this->_has_one[$property]['class'],
+ $this->get($this->_has_one[$property]['field']),
+ $this->_db_conn);
}
protected function _canHasOneObjectBeNull($property)
@@ -852,8 +890,7 @@
}
catch(Exception $e)
{
- $conn = lmbToolkit :: instance()->getDefaultDbConnection();
- $conn->rollbackTransaction();
+ $this->_db_conn->rollbackTransaction();
throw $e;
}
@@ -1075,9 +1112,10 @@
* @see find()
* @param string class name of the object
* @param mixed misc magic params
+ * @param object database connection object
* @return object|null
*/
- static function findFirst($class_name, $magic_params = array())
+ static function findFirst($class_name, $magic_params = array(), $conn = null)
{
$params = array();
if(self :: _isCriteria($magic_params))
@@ -1090,19 +1128,22 @@
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
- $obj = new $class_name();
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ $obj = new $class_name(null, $conn);
return $obj->_findFirst($params);
}
/**
- * lmbActiveRecord :: findFirst() convenience alias
+ * self :: findFirst() convenience alias
* @see findFirst()
* @param string class name of the object
* @param mixed misc magic params
* @return object|null
*/
- static function findOne($class_name, $magic_params = array())
+ static function findOne($class_name, $magic_params = array(), $conn = null)
{
- return self :: findFirst($class_name, $magic_params);
+ return self :: findFirst($class_name, $magic_params, $conn);
}
/**
* Userland filter for findFirst() static method
@@ -1112,22 +1153,26 @@
*/
protected function _findFirst($params)
{
- return lmbActiveRecord :: find(get_class($this), $params);
+ return self :: find(get_class($this), $params, $this->_db_conn);
}
/**
* Finds one instance of object in database using object id, this method is actually a wrapper around find()
* @see find()
* @param string class name of the object
* @param integer object id
+ * @param object database connection object
* @return object|null
*/
- static function findById($class_name, $id, $not_found_exception = true)
+ static function findById($class_name, $id, $throw_exception = true, $conn = null)
{
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
- $obj = new $class_name();
- return $obj->_findById($id, $not_found_exception);
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ $obj = new $class_name(null, $conn);
+ return $obj->_findById($id, $throw_exception);
}
/**
* Userland filter for findById() static method
@@ -1135,11 +1180,13 @@
* @param integer object id
* @return object
*/
- protected function _findById($id, $not_found_exception)
+ protected function _findById($id, $throw_exception)
{
- if($object = lmbActiveRecord :: find(get_class($this), array('first', 'criteria' => 'id=' . (int)$id)))
+ if($object = self :: find(get_class($this),
+ array('first', 'criteria' => 'id=' . (int)$id),
+ $this->_db_conn))
return $object;
- elseif($not_found_exception)
+ elseif($throw_exception)
throw new lmbARNotFoundException(get_class($this), $id);
else
return null;
@@ -1150,14 +1197,18 @@
* @param string class name of the object
* @param array object ids
* @param mixed misc magic params
+ * @param object database connection object
* @return iterator
*/
- static function findByIds($class_name, $ids, $params = array())
+ static function findByIds($class_name, $ids, $params = array(), $conn = null)
{
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
- $obj = new $class_name();
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ $obj = new $class_name(null, $conn);
return $obj->_findByIds($ids, $params);
}
/**
@@ -1174,7 +1225,7 @@
else
{
$params['criteria'] = new lmbSQLFieldCriteria('id', $ids, lmbSQLFieldCriteria :: IN);
- return lmbActiveRecord :: find(get_class($this), $params);
+ return self :: find(get_class($this), $params, $this->_db_conn);
}
}
/**
@@ -1185,29 +1236,32 @@
*/
function getDataset($magic_params = array())
{
- return lmbActiveRecord :: find(get_class($this), $magic_params);
+ return self :: find(get_class($this), $magic_params, $this->_db_conn);
}
/**
* Finds a collection of objects in database using raw SQL
* @param string class name of the object
* @param string SQL
+ * @param object database connection object
* @return iterator
*/
- static function findBySql($class_name, $sql)
+ static function findBySql($class_name, $sql, $conn = null)
{
- $conn = lmbToolkit :: instance()->getDefaultDbConnection();
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
$stmt = $conn->newStatement($sql);
- return lmbActiveRecord :: decorateRecordSet($stmt->getRecordSet(), $class_name);
+ return self :: decorateRecordSet($stmt->getRecordSet(), $class_name);
}
/**
* Finds first object in database using raw SQL
* @param string class name of the object
* @param string SQL
+ * @param object database connection object
* @return object
*/
- static function findFirstBySql($class_name, $sql)
+ static function findFirstBySql($class_name, $sql, $conn = null)
{
- $rs = self :: findBySql($class_name, $sql);
+ $rs = self :: findBySql($class_name, $sql, $conn);
$rs->paginate(0, 1);
$rs->rewind();
if($rs->valid())
@@ -1218,9 +1272,9 @@
* @see findFirstBySql()
* @return object
*/
- static function findOneBySql($class_name, $sql)
+ static function findOneBySql($class_name, $sql, $conn = null)
{
- return self :: findFirstBySql($class_name, $sql);
+ return self :: findFirstBySql($class_name, $sql, $conn);
}
/**
@@ -1238,31 +1292,35 @@
* // - to match 'name="hey"' criteria
* // - ordered by 'id' property using descendant sort
* // - limited to 3 items
- * $books = lmbActiveRecord :: find('Book', array('criteria' => 'name="hey"',
+ * $books = self :: find('Book', array('criteria' => 'name="hey"',
* 'sort' => array('id' => 'desc'),
* 'limit' => 3));
* //returns a collection of all Book objects in database
- * $books = lmbActiveRecord :: find('Book');
+ * $books = self :: find('Book');
* //returns one object with specified id
- * $books = lmbActiveRecord :: find('Book', 1);
+ * $books = self :: find('Book', 1);
* //returns a collection of objects which match plain text criteria
- * $books = lmbActiveRecord :: find('Book', 'name="hey"');
+ * $books = self :: find('Book', 'name="hey"');
* //returns a collection of objects which match criteria with placeholders
- * $books = lmbActiveRecord :: find('Book', array('name=? and author=?', 'hey', 'bob'));
+ * $books = self :: find('Book', array('name=? and author=?', 'hey', 'bob'));
* //returns a collection of objects which match object criteria
- * $books = lmbActiveRecord :: find('Book',
+ * $books = self :: find('Book',
* new lmbSQLFieldCriteria('name', 'hey'));
* </code>
* @param string class name of the object
* @param mixed misc magic params
+ * @param object database connection object
* @return iterator
*/
- static function find($class_name, $magic_params = array())
+ static function find($class_name, $magic_params = array(), $conn = null)
{
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
if(self :: _isCriteria($magic_params))
$params = array('criteria' => $magic_params);
elseif(is_int($magic_params))
- return self :: findById($class_name, $magic_params, false);
+ return self :: findById($class_name, $magic_params, false, $conn);
elseif(!is_array($magic_params))
throw new lmbARException("Invalid magic params", array($magic_params));
else
@@ -1271,7 +1329,7 @@
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
- $obj = new $class_name();
+ $obj = new $class_name(null, $conn);
return $obj->_find($params);
}
/**
@@ -1284,7 +1342,7 @@
{
$criteria = isset($params['criteria']) ? $params['criteria'] : null;
$sort_params = isset($params['sort']) ? $params['sort'] : array();
- $rs = $this->decorateRecordSet($this->findAllRecords($criteria, $sort_params));
+ $rs = $this->_decorateRecordSet($this->findAllRecords($criteria, $sort_params));
$return_first = false;
foreach(array_values($params) as $value)
@@ -1320,7 +1378,9 @@
if(!count($sort_params))
$sort_params = $this->_default_sort_params;
- return $this->_db_table->select($this->addClassCriteria($criteria), $sort_params, $this->_getColumnsForSelect());
+ return $this->_db_table->select($this->addClassCriteria($criteria),
+ $sort_params,
+ $this->_getColumnsForSelect());
}
/**
* Adds class name criterion to passed in criteria
@@ -1372,7 +1432,7 @@
*/
function loadById($id)
{
- $object = lmbActiveRecord :: findById(get_class($this), $id);
+ $object = self :: findById(get_class($this), $id, true, $this->_db_conn);
$this->importRaw($object->exportRaw());
$this->_resetDirty();
$this->_is_new = false;
@@ -1472,28 +1532,38 @@
* Finds all objects which satisfy the passed criteria and destroys them one by one
* @param string class name
* @param string|object search criteria, if not set all objects are removed
+ * @param object database connection object
*/
- static function delete($class_name, $criteria = null)
+ static function delete($class_name, $criteria = null, $conn = null)
{
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
$params = array();
if($criteria)
$params = array('criteria' => $criteria);
- $rs = lmbActiveRecord :: find($class_name, $params);
+ $rs = self :: find($class_name, $params, $conn);
foreach($rs as $object)
$object->destroy();
}
- function deleteRaw($class_name, $criteria = null)
+ static function deleteRaw($class_name, $criteria = null, $conn = null)
{
- $object = new $class_name();
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ $object = new $class_name(null, $conn);
$db_table = $object->getDbTable();
$db_table->delete($criteria);
}
- static function updateRaw($class_name, $set, $criteria = null)
+ static function updateRaw($class_name, $set, $criteria = null, $conn = null)
{
- $object = new $class_name();
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ $object = new $class_name(null, $conn);
$db_table = $object->getDbTable();
$db_table->update($set, $criteria);
}
@@ -1554,8 +1624,7 @@
protected function _createSQLStatement($sql)
{
- $conn = lmbToolkit :: instance()->getDefaultDbConnection();
- return $conn->newStatement($sql);
+ return $this->_db_conn->newStatement($sql);
}
protected function _query($sql)
@@ -1571,18 +1640,25 @@
}
/**
* Decorates database recordset with special decorator which converts each record into
- * corresponding lmbActiveRecord object. This method can be used both statically and as
- * instance. If used statically you must pass $class argument.
+ * corresponding lmbActiveRecord object.
* @see lmbARRecordSetDecorator
- * @todo Split this method - it is a hack since it can be called both from instance and statically
* @param iterator
- * @param string
+ * @param string wrapper class name
+ * @param object database connection object
*/
- function decorateRecordSet($rs, $class = null)
+ function decorateRecordSet($rs, $class, $conn = null)
{
- return new lmbARRecordSetDecorator($rs, $class ? $class : get_class($this));
+ if(!is_object($conn))
+ $conn = self :: getDefaultConnection();
+
+ return new lmbARRecordSetDecorator($rs, $class, $conn);
}
+ function _decorateRecordSet($rs)
+ {
+ return new lmbARRecordSetDecorator($rs, get_class($this), $this->_db_conn);
+ }
+
function __clone()
{
$this->remove('id');
@@ -1662,7 +1738,7 @@
foreach($value as $item)
{
if(is_numeric($item))
- $objects[] = new $class((int)$item);
+ $objects[] = new $class((int)$item, $this->_db_conn);
elseif(is_object($item))
$objects[] = $item;
}
@@ -1674,7 +1750,7 @@
{
if(is_numeric($value))
{
- $obj = new $class((int)$value);
+ $obj = new $class((int)$value, $this->_db_conn);
$this->set($property, $obj);
}
elseif(is_object($value))
Modified: 3.x/trunk/limb/active_record/src/toolkit/lmbARTools.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/toolkit/lmbARTools.class.php 2007-06-18 12:20:00 UTC (rev 5996)
+++ 3.x/trunk/limb/active_record/src/toolkit/lmbARTools.class.php 2007-06-18 12:27:21 UTC (rev 5997)
@@ -1,31 +1,31 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require('limb/toolkit/src/lmbAbstractTools.class.php');
-lmb_require('limb/active_record/src/lmbARMetaInfo.class.php');
-
+lmb_require('limb/active_record/src/lmbARMetaInfo.class.php');
+
/**
* class lmbARTools.
*
* @package active_record
* @version $Id$
- */
+ */
class lmbARTools extends lmbAbstractTools
{
protected $metas = array();
- function getActiveRecordMetaInfo($active_record)
+ function getActiveRecordMetaInfo($active_record, $conn = null)
{
$class_name = get_class($active_record);
if(isset($this->metas[$class_name]))
return $this->metas[$class_name];
- $meta = new lmbARMetaInfo($active_record);
+ $meta = new lmbARMetaInfo($active_record, $conn);
$this->metas[$class_name] = $meta;
return $meta;
}
More information about the limb-svn
mailing list