[limb-svn] r6315 - in 3.x/trunk/limb/active_record: src tests/cases
svn at limb-project.com
svn at limb-project.com
Wed Sep 19 15:35:40 MSD 2007
Author: pachanga
Date: 2007-09-19 15:35:40 +0400 (Wed, 19 Sep 2007)
New Revision: 6315
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6315
Modified:
3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
Log:
-- adding experimental and quite hacky ability to omit class name in find* methods, thus Foo :: find\(\) is possible instead of lmbActiveRecord :: find\(Foo\). Anyway once we have LSB patch in PHP lmbActiveRecord will have all the required infrastructure
Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php 2007-09-19 08:01:44 UTC (rev 6314)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php 2007-09-19 11:35:40 UTC (rev 6315)
@@ -1106,6 +1106,7 @@
{
foreach($params as $key => $value)
{
+ //remove obsolete check for 'first' property
if(!is_int($key) || $value == 'first')
return false;
}
@@ -1114,6 +1115,37 @@
return false;
}
+ protected static function _isClass($name)
+ {
+ if(!is_scalar($name) || !$name)
+ return false;
+
+ return ctype_alnum("$name") && !ctype_digit("$name");
+ }
+
+ protected function _getCallingClass($method, $at = 1)
+ {
+ //once PHP-5.3 LSB patch is available use a better alternative
+ //currently it's a quite a slow implementation and it doesn't
+ //recognize multiline function calls
+
+ $trace = debug_backtrace();
+ $back = $trace[$at];
+ $fp = fopen($back['file'], 'r');
+
+ for($i=0; $i<$back['line']-1; $i++)
+ fgets($fp);
+
+ $line = fgets($fp);
+ fclose($fp);
+
+ if(!preg_match('~(\w+)\s*::\s*' . $method . '\s*\(~', $line, $m))
+ throw new lmbARException("Static calling class not found!");
+ if($m[1] == 'lmbActiveRecord')
+ throw new lmbARException("Found static class can't be lmbActiveRecord!");
+ return $m[1];
+ }
+
/**
* Finds one instance of object in database, this method is actually a wrapper around find()
* @see find()
@@ -1122,16 +1154,26 @@
* @param object database connection object
* @return object|null
*/
- static function findFirst($class_name, $magic_params = array(), $conn = null)
+ static function findFirst($class_name = null, $magic_params = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $magic_params;
+ $magic_params = $class_name ? $class_name : array();
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
+
$params = array();
if(self :: _isCriteria($magic_params))
$params = array('first', 'criteria' => $magic_params);
+ elseif(is_null($magic_params))
+ $params = array('first');
elseif(is_array($magic_params))
{
$params = $magic_params;
array_push($params, 'first');
}
+
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
@@ -1148,8 +1190,14 @@
* @param mixed misc magic params
* @return object|null
*/
- static function findOne($class_name, $magic_params = array(), $conn = null)
+ static function findOne($class_name = null, $magic_params = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $magic_params;
+ $magic_params = $class_name ? $class_name : array();
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
return self :: findFirst($class_name, $magic_params, $conn);
}
/**
@@ -1207,8 +1255,16 @@
* @param object database connection object
* @return iterator
*/
- static function findByIds($class_name, $ids, $params = array(), $conn = null)
+ static function findByIds($class_name, $ids = null, $params = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $params;
+ $params = $ids;
+ $ids = $class_name;
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
+
if(!class_exists($class_name, true))
throw new lmbARException("Could not find class '$class_name'");
@@ -1252,10 +1308,18 @@
* @param object database connection object
* @return iterator
*/
- static function findBySql($class_name, $sql, $conn = null)
+ static function findBySql($class_name, $sql = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $sql;
+ $sql = $class_name;
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
+
if(!is_object($conn))
$conn = self :: getDefaultConnection();
+
$stmt = $conn->newStatement($sql);
return self :: decorateRecordSet($stmt->getRecordSet(), $class_name);
}
@@ -1266,8 +1330,15 @@
* @param object database connection object
* @return object
*/
- static function findFirstBySql($class_name, $sql, $conn = null)
+ static function findFirstBySql($class_name, $sql = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $sql;
+ $sql = $class_name;
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
+
$rs = self :: findBySql($class_name, $sql, $conn);
$rs->paginate(0, 1);
$rs->rewind();
@@ -1279,8 +1350,14 @@
* @see findFirstBySql()
* @return object
*/
- static function findOneBySql($class_name, $sql, $conn = null)
+ static function findOneBySql($class_name, $sql = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $sql;
+ $sql = $class_name;
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
return self :: findFirstBySql($class_name, $sql, $conn);
}
@@ -1319,8 +1396,15 @@
* @param object database connection object
* @return iterator
*/
- static function find($class_name, $magic_params = array(), $conn = null)
+ static function find($class_name = null, $magic_params = null, $conn = null)
{
+ if(!self :: _isClass($class_name))
+ {
+ $conn = $magic_params;
+ $magic_params = $class_name ? $class_name : array();
+ $class_name = self :: _getCallingClass(__FUNCTION__);
+ }
+
if(!is_object($conn))
$conn = self :: getDefaultConnection();
@@ -1328,6 +1412,8 @@
$params = array('criteria' => $magic_params);
elseif(is_int($magic_params))
return self :: findById($class_name, $magic_params, false, $conn);
+ elseif(is_null($magic_params))
+ $params = array();
elseif(!is_array($magic_params))
throw new lmbARException("Invalid magic params", array($magic_params));
else
Modified: 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php 2007-09-19 08:01:44 UTC (rev 6314)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php 2007-09-19 11:35:40 UTC (rev 6315)
@@ -7,6 +7,7 @@
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
require_once('limb/active_record/src/lmbActiveRecord.class.php');
+require_once('limb/dbal/src/criteria/lmbSQLRawCriteria.class.php');
require_once('limb/dbal/src/lmbSimpleDb.class.php');
require_once('limb/dbal/src/lmbTableGateway.class.php');
@@ -270,7 +271,7 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $object3 = lmbActiveRecord :: findById($this->class_name, $object2->getId());
+ $object3 = lmbActiveRecord :: findById('TestOneTableObject', $object2->getId());
$this->assertEqual($object3->export(), $object2->export());
}
@@ -278,7 +279,7 @@
{
try
{
- lmbActiveRecord :: findById($this->class_name, -1000);
+ lmbActiveRecord :: findById('TestOneTableObject', -1000);
$this->assertTrue(false);
}
catch(lmbARException $e){}
@@ -286,7 +287,7 @@
function testFindByIdReturnsNullIfNotFound()
{
- $this->assertNull(lmbActiveRecord :: findById($this->class_name, -1000, false));
+ $this->assertNull(lmbActiveRecord :: findById('TestOneTableObject', -1000, false));
}
function testLoadById()
@@ -309,56 +310,80 @@
$this->assertFalse($object2->isNew());
}
- function tesFindFirst()
+ function testFindFirst()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $this->assertFalse($object2->isNew());
- $object3 = lmbActiveRecord :: findFirst($this->class_name, array('criteria' => 'id=' . $object1->getId()));
+ $object3 = lmbActiveRecord :: findFirst('TestOneTableObject', array('criteria' => 'id=' . $object1->getId()));
+ $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
+ $this->assertEqual($object3->get('content'), $object1->get('content'));
+ $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
+ $this->assertEqual($object3->get('id'), $object1->getId());
- $this->assertFalse($object2->isNew());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findFirst(array('criteria' => 'id=' . $object1->getId()));
$this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
$this->assertEqual($object3->get('content'), $object1->get('content'));
$this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
$this->assertEqual($object3->get('id'), $object1->getId());
}
- function tesFindFirstConvertStringToCriteria()
+ function testFindFirstConvertStringToCriteria()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $this->assertFalse($object2->isNew());
- $object3 = lmbActiveRecord :: findFirst($this->class_name, 'id=' . $object1->getId());
+ $object3 = lmbActiveRecord :: findFirst('TestOneTableObject', 'id=' . $object1->getId());
+ $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
+ $this->assertEqual($object3->get('content'), $object1->get('content'));
+ $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
+ $this->assertEqual($object3->get('id'), $object1->getId());
- $this->assertFalse($object2->isNew());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findFirst('id=' . $object1->getId());
$this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
$this->assertEqual($object3->get('content'), $object1->get('content'));
$this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
$this->assertEqual($object3->get('id'), $object1->getId());
}
- function tesFindFirstConvertObjectToCriteria()
+ function testFindFirstConvertObjectToCriteria()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $this->assertFalse($object2->isNew());
- $object3 = lmbActiveRecord :: findFirst($this->class_name, new lmbSQLRawCriteria('id=' . $object1->getId()));
+ $object3 = lmbActiveRecord :: findFirst('TestOneTableObject', new lmbSQLRawCriteria('id=' . $object1->getId()));
+ $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
+ $this->assertEqual($object3->get('content'), $object1->get('content'));
+ $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
+ $this->assertEqual($object3->get('id'), $object1->getId());
- $this->assertFalse($object2->isNew());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findFirst(new lmbSQLRawCriteria('id=' . $object1->getId()));
$this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
$this->assertEqual($object3->get('content'), $object1->get('content'));
$this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
$this->assertEqual($object3->get('id'), $object1->getId());
}
- function tesFindFirstConvertArrayToCriteria()
+ function testFindFirstConvertArrayToCriteria()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $this->assertFalse($object2->isNew());
- $object3 = lmbActiveRecord :: findFirst($this->class_name, array('id=?', $object1->getId()));
+ $object3 = lmbActiveRecord :: findFirst('TestOneTableObject', array('id=?', $object1->getId()));
+ $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
+ $this->assertEqual($object3->get('content'), $object1->get('content'));
+ $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
+ $this->assertEqual($object3->get('id'), $object1->getId());
- $this->assertFalse($object2->isNew());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findFirst(array('id=?', $object1->getId()));
$this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
$this->assertEqual($object3->get('content'), $object1->get('content'));
$this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
@@ -370,8 +395,11 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $object3 = lmbActiveRecord :: findFirst($this->class_name, array('sort' => array('id' => 'DESC')));
+ $object3 = lmbActiveRecord :: findFirst('TestOneTableObject', array('sort' => array('id' => 'DESC')));
+ $this->assertEqual($object3->get('id'), $object2->getId());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findFirst(array('sort' => array('id' => 'DESC')));
$this->assertEqual($object3->get('id'), $object2->getId());
}
@@ -387,16 +415,26 @@
$object3 = lmbActiveRecord :: findFirst('TestOneTableObjectWithSortParams');
$this->assertEqual($object3->get('id'), $object2->getId());
+
+ //testing convenient alias
+ $object3 = TestOneTableObjectWithSortParams :: findFirst();
+ $this->assertEqual($object3->get('id'), $object2->getId());
}
function testFindOneAlias()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $this->assertFalse($object2->isNew());
- $object3 = lmbActiveRecord :: findOne($this->class_name, 'id=' . $object1->getId());
+ $object3 = lmbActiveRecord :: findOne('TestOneTableObject', 'id=' . $object1->getId());
+ $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
+ $this->assertEqual($object3->get('content'), $object1->get('content'));
+ $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
+ $this->assertEqual($object3->get('id'), $object1->getId());
- $this->assertFalse($object2->isNew());
+ //testing convenient alias
+ $object3 = TestOneTableObject :: findOne('id=' . $object1->getId());
$this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
$this->assertEqual($object3->get('content'), $object1->get('content'));
$this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
@@ -434,11 +472,18 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name);
+ $rs = lmbActiveRecord :: find('TestOneTableObject');
$rs->rewind();
$this->assertEqual($object1->getId(), $rs->current()->getId());
$rs->next();
$this->assertEqual($object2->getId(), $rs->current()->getId());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find();
+ $rs->rewind();
+ $this->assertEqual($object1->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
}
function testFindAllWithCriteria()
@@ -446,11 +491,18 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name, array('criteria' => new lmbSQLFieldCriteria('id', $object2->getId())));
+ $rs = lmbActiveRecord :: find('TestOneTableObject', array('criteria' => new lmbSQLFieldCriteria('id', $object2->getId())));
$rs->rewind();
$this->assertEqual($object2->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find(array('criteria' => new lmbSQLFieldCriteria('id', $object2->getId())));
+ $rs->rewind();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
function testFindConvertObjectToCriteria()
@@ -458,11 +510,18 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name, new lmbSQLFieldCriteria('id', $object2->getId()));
+ $rs = lmbActiveRecord :: find('TestOneTableObject', new lmbSQLFieldCriteria('id', $object2->getId()));
$rs->rewind();
$this->assertEqual($object2->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find(new lmbSQLFieldCriteria('id', $object2->getId()));
+ $rs->rewind();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
function testFindConvertStringToCriteria()
@@ -470,11 +529,18 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name, 'id=' . $object2->getId());
+ $rs = lmbActiveRecord :: find('TestOneTableObject', 'id=' . $object2->getId());
$rs->rewind();
$this->assertEqual($object2->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find('id=' . $object2->getId());
+ $rs->rewind();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
function testFindConvertArrayToCriteria()
@@ -482,11 +548,18 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name, array('id=?', $object2->getId()));
+ $rs = lmbActiveRecord :: find('TestOneTableObject', array('id=?', $object2->getId()));
$rs->rewind();
$this->assertEqual($object2->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find(array('id=?', $object2->getId()));
+ $rs->rewind();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
function testFindWithIntegerCallsFindById()
@@ -494,35 +567,37 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $object = lmbActiveRecord :: find($this->class_name, $object2->getId());
+ $object = lmbActiveRecord :: find('TestOneTableObject', $object2->getId());
$this->assertEqual($object2->getId(), $object->getId());
+
+ //testing convenient alias
+ $object = TestOneTableObject :: find($object2->getId());
+ $this->assertEqual($object2->getId(), $object->getId());
}
function testFindWithIntegerDoesNotThrowException()
{
- $this->assertNull(lmbActiveRecord :: find($this->class_name, -10000));
- }
+ $this->assertNull(lmbActiveRecord :: find('TestOneTableObject', -10000));
- function testFindByThrowsExceptionIfMagicParamsIsNull()
- {
- try
- {
- lmbActiveRecord :: find($this->class_name, null);
- $this->assertTrue(false);
- }
- catch(lmbARException $e){}
+ //testing convenient alias
+ $this->assertNull(TestOneTableObject :: find(-10000));
}
-
function testFindAllWithSortParams()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: find($this->class_name, array('sort' => array('id' => 'DESC')));
+ $rs = lmbActiveRecord :: find('TestOneTableObject', array('sort' => array('id' => 'DESC')));
$arr = $rs->getArray();
$this->assertEqual($arr[0]->get('id'), $object2->getId());
$this->assertEqual($arr[1]->get('id'), $object1->getId());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find(array('sort' => array('id' => 'DESC')));
+ $arr = $rs->getArray();
+ $this->assertEqual($arr[0]->get('id'), $object2->getId());
+ $this->assertEqual($arr[1]->get('id'), $object1->getId());
}
function testFindAllWithDefaultSortParams()
@@ -539,6 +614,12 @@
$arr = $rs->getArray();
$this->assertEqual($arr[0]->get('id'), $object2->getId());
$this->assertEqual($arr[1]->get('id'), $object1->getId());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: find(array('sort' => array('id' => 'DESC')));
+ $arr = $rs->getArray();
+ $this->assertEqual($arr[0]->get('id'), $object2->getId());
+ $this->assertEqual($arr[1]->get('id'), $object1->getId());
}
function testFindBySql()
@@ -546,13 +627,22 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: findBySql($this->class_name, 'select * from test_one_table_object order by id desc');
+ $rs = lmbActiveRecord :: findBySql('TestOneTableObject', 'select * from test_one_table_object order by id desc');
$rs->rewind();
$this->assertEqual($object2->getId(), $rs->current()->getId());
$rs->next();
$this->assertEqual($object1->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: findBySql('select * from test_one_table_object order by id desc');
+ $rs->rewind();
+ $this->assertEqual($object2->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertEqual($object1->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
function testFindFirstBySql()
@@ -560,17 +650,34 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $object = lmbActiveRecord :: findFirstBySql($this->class_name, 'select * from test_one_table_object order by id desc');
+ $object = lmbActiveRecord :: findFirstBySql('TestOneTableObject', 'select * from test_one_table_object order by id desc');
$this->assertEqual($object2->getId(), $object->getId());
+
+ //testing convenient alias
+ $object = TestOneTableObject :: findFirstBySql('select * from test_one_table_object order by id desc');
+ $this->assertEqual($object2->getId(), $object->getId());
}
+ function testFindOneBySqlAlias()
+ {
+ $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+ $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
+
+ $object = lmbActiveRecord :: findOneBySql('TestOneTableObject', 'select * from test_one_table_object order by id desc');
+ $this->assertEqual($object2->getId(), $object->getId());
+
+ //testing convenient alias
+ $object = TestOneTableObject :: findOneBySql('select * from test_one_table_object order by id desc');
+ $this->assertEqual($object2->getId(), $object->getId());
+ }
+
function testFindByIds()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object3 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: findByIds($this->class_name,
+ $rs = lmbActiveRecord :: findByIds('TestOneTableObject',
array($object1->getId(), $object3->getId()),
array('sort' => array('id' => 'asc')));
$rs->rewind();
@@ -579,16 +686,30 @@
$this->assertEqual($object3->getId(), $rs->current()->getId());
$rs->next();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: findByIds(array($object1->getId(), $object3->getId()), array('sort' => array('id' => 'asc')));
+ $rs->rewind();
+ $this->assertEqual($object1->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertEqual($object3->getId(), $rs->current()->getId());
+ $rs->next();
+ $this->assertFalse($rs->valid());
}
- function testFetchByIdsReturnEmptyIteratorIfNoIds()
+ function testFindByIdsReturnEmptyIteratorIfNoIds()
{
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- $rs = lmbActiveRecord :: findByIds($this->class_name, array());
+ $rs = lmbActiveRecord :: findByIds('TestOneTableObject', array());
$rs->rewind();
$this->assertFalse($rs->valid());
+
+ //testing convenient alias
+ $rs = TestOneTableObject :: findByIds(array());
+ $rs->rewind();
+ $this->assertFalse($rs->valid());
}
function testGetDatasetActsAsStaticFind()
@@ -606,8 +727,7 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- lmbActiveRecord :: delete($this->class_name);
-
+ lmbActiveRecord :: delete('TestOneTableObject');
$this->assertEqual($this->db->count('test_one_table_object'), 0);
}
@@ -631,11 +751,11 @@
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$criteria = new lmbSQLFieldCriteria('id', $object2->getId());
- lmbActiveRecord :: delete($this->class_name, $criteria);
+ lmbActiveRecord :: delete('TestOneTableObject', $criteria);
$this->assertEqual($this->db->count('test_one_table_object'), 1);
- $object3 = lmbActiveRecord :: findById($this->class_name, $object1->getId());
+ $object3 = lmbActiveRecord :: findById('TestOneTableObject', $object1->getId());
$this->assertEqual($object3->getContent(), $object1->getContent());
}
@@ -644,7 +764,7 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- lmbActiveRecord :: deleteRaw($this->class_name);
+ lmbActiveRecord :: deleteRaw('TestOneTableObject');
$this->assertEqual($this->db->count('test_one_table_object'), 0);
}
@@ -669,11 +789,11 @@
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$criteria = new lmbSQLFieldCriteria('id', $object2->getId());
- lmbActiveRecord :: deleteRaw($this->class_name, $criteria);
+ lmbActiveRecord :: deleteRaw('TestOneTableObject', $criteria);
$this->assertEqual($this->db->count('test_one_table_object'), 1);
- $object3 = lmbActiveRecord :: findById($this->class_name, $object1->getId());
+ $object3 = lmbActiveRecord :: findById('TestOneTableObject', $object1->getId());
$this->assertEqual($object3->getContent(), $object1->getContent());
}
@@ -682,9 +802,9 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- lmbActiveRecord :: updateRaw($this->class_name, array('content' => 'blah'));
+ lmbActiveRecord :: updateRaw('TestOneTableObject', array('content' => 'blah'));
- $rs = lmbActiveRecord :: find($this->class_name);
+ $rs = lmbActiveRecord :: find('TestOneTableObject');
$rs->rewind();
$this->assertEqual($rs->current()->getContent(), 'blah');
$rs->next();
@@ -698,9 +818,9 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- lmbActiveRecord :: updateRaw($this->class_name, 'ordr=1');
+ lmbActiveRecord :: updateRaw('TestOneTableObject', 'ordr=1');
- $rs = lmbActiveRecord :: find($this->class_name);
+ $rs = lmbActiveRecord :: find('TestOneTableObject');
$rs->rewind();
$this->assertEqual($rs->current()->getOrdr(), 1);
$rs->next();
@@ -714,9 +834,9 @@
$object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
$object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
- lmbActiveRecord :: updateRaw($this->class_name, array('content' => 'blah'), 'id=' . $object2->getId());
+ lmbActiveRecord :: updateRaw('TestOneTableObject', array('content' => 'blah'), 'id=' . $object2->getId());
- $rs = lmbActiveRecord :: find($this->class_name);
+ $rs = lmbActiveRecord :: find('TestOneTableObject');
$rs->rewind();
$this->assertEqual($rs->current()->getContent(), $object1->getContent());
$rs->next();
More information about the limb-svn
mailing list