diff --git a/framework/db/ar/ActiveQuery.php b/framework/db/ar/ActiveQuery.php index 5a10953..24dcde6 100644 --- a/framework/db/ar/ActiveQuery.php +++ b/framework/db/ar/ActiveQuery.php @@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array * public $with; */ - if ($this->query->from === null) { - $modelClass = $this->modelClass; - $this->query->from = $modelClass::tableName(); - if ($this->tableAlias !== null) { - $this->query->from .= $this->tableAlias; + if ($this->sql === null) { + if ($this->query->from === null) { + $modelClass = $this->modelClass; + $tableName = $modelClass::tableName(); + 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(); + if ($this->asArray) { if ($this->indexBy === null) { return $rows; diff --git a/tests/unit/MysqlTestCase.php b/tests/unit/MysqlTestCase.php index 8c0fbcb..d897348 100644 --- a/tests/unit/MysqlTestCase.php +++ b/tests/unit/MysqlTestCase.php @@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase function getConnection($reset = true) { $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) { $db->active = true; $lines = explode(';', file_get_contents($params['fixture'])); diff --git a/tests/unit/data/ar/ActiveRecord.php b/tests/unit/data/ar/ActiveRecord.php new file mode 100644 index 0000000..aafc99f --- /dev/null +++ b/tests/unit/data/ar/ActiveRecord.php @@ -0,0 +1,28 @@ + + * @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 diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php new file mode 100644 index 0000000..ed40c4d --- /dev/null +++ b/tests/unit/data/ar/Customer.php @@ -0,0 +1,11 @@ +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 diff --git a/tests/unit/framework/db/dao/ConnectionTest.php b/tests/unit/framework/db/dao/ConnectionTest.php index 0e4d9f6..c1c7ac0 100644 --- a/tests/unit/framework/db/dao/ConnectionTest.php +++ b/tests/unit/framework/db/dao/ConnectionTest.php @@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase $this->assertFalse($connection->active); $this->assertEquals(null, $connection->pdo); - $connection = new Connection('unknown::memory:'); + $connection = new Connection; + $connection->dsn = 'unknown::memory:'; $this->setExpectedException('yii\db\Exception'); $connection->open(); } diff --git a/tests/unit/framework/db/dao/QueryTest.php b/tests/unit/framework/db/dao/QueryTest.php index 05b9f86..cbfd2ff 100644 --- a/tests/unit/framework/db/dao/QueryTest.php +++ b/tests/unit/framework/db/dao/QueryTest.php @@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase $query = new Query; $query->select(); $this->assertEquals('*', $query->select); - $this->assertFalse($query->distinct); + $this->assertNull($query->distinct); $this->assertEquals(null, $query->selectOption); $query = new Query; - $query->select('id, name', true, 'something'); + $query->select('id, name', 'something')->distinct(true); $this->assertEquals('id, name', $query->select); $this->assertTrue($query->distinct); $this->assertEquals('something', $query->selectOption);