Commit 376dd0d4 by Qiang Xue

..

parent 1978a4ef
...@@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array ...@@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* public $with; * public $with;
*/ */
if ($this->query->from === null) { if ($this->sql === null) {
$modelClass = $this->modelClass; if ($this->query->from === null) {
$this->query->from = $modelClass::tableName(); $modelClass = $this->modelClass;
if ($this->tableAlias !== null) { $tableName = $modelClass::tableName();
$this->query->from .= $this->tableAlias; if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$this->query->from = array($tableName);
} }
$command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql();
} else {
$command = $this->getDbConnection()->createCommand($this->sql);
$command->bindValues($this->query->params);
} }
$command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql();
$rows = $command->queryAll(); $rows = $command->queryAll();
if ($this->asArray) { if ($this->asArray) {
if ($this->indexBy === null) { if ($this->indexBy === null) {
return $rows; return $rows;
......
...@@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase ...@@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase
function getConnection($reset = true) function getConnection($reset = true)
{ {
$params = $this->getParam('mysql'); $params = $this->getParam('mysql');
$db = new \yii\db\dao\Connection($params['dsn'], $params['username'], $params['password']); $db = new \yii\db\dao\Connection;
$db->dsn = $params['dsn'];
$db->username = $params['username'];
$db->password = $params['password'];
if ($reset) { if ($reset) {
$db->active = true; $db->active = true;
$lines = explode(';', file_get_contents($params['fixture'])); $lines = explode(';', file_get_contents($params['fixture']));
......
<?php
/**
* ActiveRecord class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\ar;
use yii\db\dao\Connection;
/**
* ActiveRecord is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveRecord extends \yii\db\ar\ActiveRecord
{
public static $db;
public static function getDbConnection()
{
return self::$db;
}
}
\ No newline at end of file
<?php
namespace yiiunit\data\ar;
class Customer extends ActiveRecord
{
public static function tableName()
{
return 'tbl_customer';
}
}
\ No newline at end of file
<?php
namespace yiiunit\data\ar;
class Order extends ActiveRecord
{
public static function tableName()
{
return 'tbl_order';
}
}
\ No newline at end of file
<?php
namespace yiiunit\framework\db\ar;
use yii\db\dao\Query;
use yii\db\ar\ActiveQuery;
use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer;
class ActiveRecordTest extends \yiiunit\MysqlTestCase
{
public function setUp()
{
ActiveRecord::$db = $this->getConnection();
}
public function testFind()
{
// find one
$result = Customer::find();
$this->assertTrue($result instanceof ActiveQuery);
$customer = $result->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(1, $result->count);
// find all
$result = Customer::find();
$customers = $result->all();
$this->assertTrue(is_array($customers));
$this->assertEquals(3, count($customers));
$this->assertTrue($customers[0] instanceof Customer);
$this->assertTrue($customers[1] instanceof Customer);
$this->assertTrue($customers[2] instanceof Customer);
$this->assertEquals(3, $result->count);
// check count first
$result = Customer::find();
$this->assertEquals(3, $result->count);
$customer = $result->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(1, $result->count);
// iterator
$result = Customer::find();
$count = 0;
foreach ($result as $customer) {
$this->assertTrue($customer instanceof Customer);
$count++;
}
$this->assertEquals($count, $result->count);
// array access
$result = Customer::find();
$this->assertTrue($result[0] instanceof Customer);
$this->assertTrue($result[1] instanceof Customer);
$this->assertTrue($result[2] instanceof Customer);
// find by a single primary key
$customer = Customer::find(2)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
// find by attributes
$customer = Customer::find(array('name'=>'user2'))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(2, $customer->id);
// find by Query
$query = new ActiveQuery;
$query->where('id=:id', array(':id'=>2));
$customer = Customer::find($query)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
}
public function testFindBySql()
{
// find one
$customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user3', $customer->name);
// find all
$customers = Customer::findBySql('SELECT * FROM tbl_customer')->all();
$this->assertEquals(3, count($customers));
// find with parameter binding
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id'=>2))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
// count
$finder = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
$finder->one();
$this->assertEquals(1, $finder->count);
$finder = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
// todo
// $this->assertEquals(3, $finder->count);
}
public function testQueryMethods()
{
$customer = Customer::find()->where('id=?', 2)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
$customer = Customer::find()->where(array('name' => 'user3'))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user3', $customer->name);
$customer = Customer::find()->select('id')->orderBy('id DESC')->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, $customer->id);
$this->assertEquals(null, $customer->name);
}
public function testGetSql()
{
// sql for all
$sql = Customer::find()->sql;
$this->assertEquals('SELECT * FROM tbl_customer', $sql);
// sql for one row
$sql = Customer::find()->oneSql;
$this->assertEquals('SELECT * FROM tbl_customer LIMIT 1', $sql);
// sql for count
$sql = Customer::find()->countSql;
$this->assertEquals('SELECT COUNT(*) FROM tbl_customer', $sql);
}
public function testArrayResult()
{
Customer::find()->asArray()->one();
Customer::find()->asArray()->all();
}
public function testMisc()
{
/*
* Customer::exists()
* Customer::updateAll()
* Customer::updateCounters()
* Customer::deleteAll()
*/
}
public function testInsert()
{
}
public function testUpdate()
{
}
public function testDelete()
{
}
public function testLazyLoading()
{
}
public function testEagerLoading()
{
}
}
\ No newline at end of file
...@@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase ...@@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
$this->assertFalse($connection->active); $this->assertFalse($connection->active);
$this->assertEquals(null, $connection->pdo); $this->assertEquals(null, $connection->pdo);
$connection = new Connection('unknown::memory:'); $connection = new Connection;
$connection->dsn = 'unknown::memory:';
$this->setExpectedException('yii\db\Exception'); $this->setExpectedException('yii\db\Exception');
$connection->open(); $connection->open();
} }
......
...@@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase ...@@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase
$query = new Query; $query = new Query;
$query->select(); $query->select();
$this->assertEquals('*', $query->select); $this->assertEquals('*', $query->select);
$this->assertFalse($query->distinct); $this->assertNull($query->distinct);
$this->assertEquals(null, $query->selectOption); $this->assertEquals(null, $query->selectOption);
$query = new Query; $query = new Query;
$query->select('id, name', true, 'something'); $query->select('id, name', 'something')->distinct(true);
$this->assertEquals('id, name', $query->select); $this->assertEquals('id, name', $query->select);
$this->assertTrue($query->distinct); $this->assertTrue($query->distinct);
$this->assertEquals('something', $query->selectOption); $this->assertEquals('something', $query->selectOption);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment