Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
2f37d09367
  1. 2
      framework/base/Dictionary.php
  2. 23
      framework/base/Object.php
  3. 2
      framework/base/Vector.php
  4. 21
      framework/db/ar/ActiveQueryBuilder.php
  5. 1778
      framework/db/ar/ActiveRecord.php
  6. 16
      framework/db/dao/TableSchema.php
  7. 20
      framework/util/Text.php
  8. 250
      tests/unit/data/mysql.sql
  9. 4
      tests/unit/framework/base/DictionaryTest.php
  10. 1
      tests/unit/framework/base/ObjectTest.php
  11. 4
      tests/unit/framework/base/VectorTest.php
  12. 100
      tests/unit/framework/db/dao/CommandTest.php

2
framework/base/Dictionary.php

@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an iterator. * @throws Exception if data is neither an array nor an iterator.
*/ */
public function fromArray($data) public function copyFrom($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_d !== array()) { if ($this->_d !== array()) {

23
framework/base/Object.php

@ -342,27 +342,4 @@ class Object
return $object; return $object;
} }
/**
* Configures the object properties with the specified array.
* @param array $array name-value pairs to be used to initialize the properties of this object.
* @return Object the object itself
*/
public function fromArray($array)
{
foreach ($array as $name => $value) {
$this->$name = $value;
}
return $this;
}
/**
* Returns the object in terms of an array.
* The default implementation will return the result of PHP function `get_object_vars()`.
* @return array the array representation of this object.
*/
public function toArray()
{
return get_object_vars($this);
}
} }

2
framework/base/Vector.php

@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an object implementing `Traversable`. * @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/ */
public function fromArray($data) public function copyFrom($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_c > 0) { if ($this->_c > 0) {

21
framework/db/ar/ActiveQueryBuilder.php

@ -0,0 +1,21 @@
<?php
/**
* ActiveQueryBuilder class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
/**
* ActiveQueryBuilder is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveQueryBuilder extends \yii\base\Object
{
}

1778
framework/db/ar/ActiveRecord.php

File diff suppressed because it is too large Load Diff

16
framework/db/dao/TableSchema.php

@ -10,6 +10,8 @@
namespace yii\db\dao; namespace yii\db\dao;
use yii\db\Exception;
/** /**
* TableSchema represents the metadata of a database table. * TableSchema represents the metadata of a database table.
* *
@ -82,4 +84,18 @@ class TableSchema extends \yii\base\Object
{ {
return array_keys($this->columns); return array_keys($this->columns);
} }
public function fixPrimaryKey($keys)
{
if (!is_array($keys)) {
$keys = array($keys);
}
foreach ($keys as $key) {
if (isset($this->columns[$key])) {
$this->columns[$key]->isPrimaryKey = true;
} else {
throw new Exception("Primary key '$key' cannot be found in table '{$this->name}'.");
}
}
}
} }

20
framework/util/Text.php

@ -14,6 +14,8 @@ namespace yii\util;
/** /**
* Text helper * Text helper
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0 * @since 2.0
*/ */
class Text class Text
@ -23,9 +25,9 @@ class Text
* @param string $name the word to be pluralized * @param string $name the word to be pluralized
* @return string the pluralized word * @return string the pluralized word
*/ */
public function pluralize($name) public static function pluralize($name)
{ {
$rules=array( $rules = array(
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es', '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/(m)an$/i' => '\1en', '/(m)an$/i' => '\1en',
@ -33,11 +35,17 @@ class Text
'/(r)y$/i' => '\1ies', '/(r)y$/i' => '\1ies',
'/s$/' => 's', '/s$/' => 's',
); );
foreach($rules as $rule=>$replacement) foreach ($rules as $rule => $replacement)
{ {
if(preg_match($rule,$name)) if (preg_match($rule, $name)) {
return preg_replace($rule,$replacement,$name); return preg_replace($rule, $replacement, $name);
}
} }
return $name.'s'; return $name . 's';
}
public static function dd($value)
{
return trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $value))));
} }
} }

250
tests/unit/data/mysql.sql

@ -1,160 +1,96 @@
/** /**
* This is the database schema for testing MySQL support of yii Active Record. * This is the database schema for testing MySQL support of Yii DAO and Active Record.
* To test this feature, you need to create a database named 'yii' on 'localhost' * The following database setup is required to perform then relevant tests:
* and create an account 'test/test' which owns this test database. * Database name: yiitest
* username: test
* password: test
* charset: utf8
*/ */
DROP TABLE IF EXISTS yii_type CASCADE; DROP TABLE IF EXISTS tbl_order_item CASCADE;
DROP TABLE IF EXISTS yii_item CASCADE; DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS yii_order CASCADE; DROP TABLE IF EXISTS tbl_order CASCADE;
DROP TABLE IF EXISTS yii_post_category CASCADE; DROP TABLE IF EXISTS tbl_category CASCADE;
DROP TABLE IF EXISTS yii_category CASCADE; DROP TABLE IF EXISTS tbl_customer CASCADE;
DROP TABLE IF EXISTS yii_comment CASCADE; DROP TABLE IF EXISTS tbl_type CASCADE;
DROP TABLE IF EXISTS yii_post CASCADE;
DROP TABLE IF EXISTS yii_profile CASCADE; CREATE TABLE `tbl_customer` (
DROP TABLE IF EXISTS yii_user CASCADE; `id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL,
CREATE TABLE yii_user `name` varchar(128) NOT NULL,
( `address` text,
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, PRIMARY KEY (`id`)
username VARCHAR(128) NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
password VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL CREATE TABLE `tbl_category` (
) TYPE=INNODB; `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
INSERT INTO yii_user (username, password, email) VALUES ('user1','pass1','email1'); PRIMARY KEY (`id`)
INSERT INTO yii_user (username, password, email) VALUES ('user2','pass2','email2'); ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO yii_user (username, password, email) VALUES ('user3','pass3','email3');
CREATE TABLE `tbl_item` (
CREATE TABLE yii_profile `id` int(11) NOT NULL AUTO_INCREMENT,
( `name` varchar(128) NOT NULL,
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, `category_id` int(11) NOT NULL,
first_name VARCHAR(128) NOT NULL, PRIMARY KEY (`id`),
last_name VARCHAR(128) NOT NULL, KEY `FK_item_category_id` (`category_id`),
user_id INTEGER NOT NULL, CONSTRAINT `FK_item_category_id` FOREIGN KEY (`category_id`) REFERENCES `tbl_category` (`id`) ON DELETE CASCADE
CONSTRAINT FK_profile_user FOREIGN KEY (user_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB; CREATE TABLE `tbl_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
INSERT INTO yii_profile (first_name, last_name, user_id) VALUES ('first 1','last 1',1); `customer_id` int(11) NOT NULL,
INSERT INTO yii_profile (first_name, last_name, user_id) VALUES ('first 2','last 2',2); `create_time` int(11) NOT NULL,
`total` decimal(10,0) NOT NULL,
CREATE TABLE yii_post PRIMARY KEY (`id`)
( ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(128) NOT NULL, CREATE TABLE `tbl_order_item` (
create_time INTEGER NOT NULL, `order_id` int(11) NOT NULL,
author_id INTEGER NOT NULL, `item_id` int(11) NOT NULL,
content TEXT, `quantity` int(11) NOT NULL,
CONSTRAINT FK_post_author FOREIGN KEY (author_id) `subtotal` decimal(10,0) NOT NULL,
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT PRIMARY KEY (`order_id`,`item_id`),
) TYPE=INNODB; KEY `FK_order_item_item_id` (`item_id`),
CONSTRAINT `FK_order_item_order_id` FOREIGN KEY (`order_id`) REFERENCES `tbl_order` (`id`) ON DELETE CASCADE,
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 1','1324854194',1,'content 1'); CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `tbl_item` (`id`) ON DELETE CASCADE
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 2','1324855194',2,'content 2'); ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 3','1324856194',2,'content 3');
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 4','1324857194',2,'content 4'); CREATE TABLE `tbl_type` (
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 5','1324858194',3,'content 5'); `int_col` int(11) NOT NULL,
`int_col2` int(11) DEFAULT '1',
CREATE TABLE yii_comment `char_col` char(100) NOT NULL,
( `char_col2` varchar(100) DEFAULT 'something',
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, `char_col3` text,
content TEXT NOT NULL, `float_col` double(4,3) NOT NULL,
post_id INTEGER NOT NULL, `float_col2` double DEFAULT '1.23',
author_id INTEGER NOT NULL, `blob_col` blob,
CONSTRAINT FK_post_comment FOREIGN KEY (post_id) `numeric_col` decimal(5,2) DEFAULT '33.22',
REFERENCES yii_post (id) ON DELETE CASCADE ON UPDATE RESTRICT, `time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
CONSTRAINT FK_user_comment FOREIGN KEY (author_id) `bool_col` tinyint(1) NOT NULL,
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT `bool_col2` tinyint(1) DEFAULT '1'
) TYPE=INNODB; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 1',1, 2); INSERT INTO tbl_customer (email, name, address) VALUES ('user1@example.com', 'user1', 'address1');
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 2',1, 2); INSERT INTO tbl_customer (email, name, address) VALUES ('user2@example.com', 'user2', 'address2');
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 3',1, 2); INSERT INTO tbl_customer (email, name, address) VALUES ('user3@example.com', 'user3', 'address3');
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 4',2, 2);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 5',2, 2); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 6',3, 2); INSERT INTO tbl_category (name) VALUES ('Movies');
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 7',3, 2);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 8',3, 2); INSERT INTO tbl_item (name, category_id) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 9',3, 2); INSERT INTO tbl_item (name, category_id) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 10',5, 3); INSERT INTO tbl_item (name, category_id) VALUES ('Ice Age', 2);
INSERT INTO tbl_item (name, category_id) VALUES ('Toy Story', 2);
CREATE TABLE yii_category INSERT INTO tbl_item (name, category_id) VALUES ('Cars', 2);
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, INSERT INTO tbl_order (customer_id, create_time, total) VALUES (1, 1325282384, 110.0);
name VARCHAR(128) NOT NULL, INSERT INTO tbl_order (customer_id, create_time, total) VALUES (2, 1325334482, 33.0);
parent_id INTEGER, INSERT INTO tbl_order (customer_id, create_time, total) VALUES (3, 1325502201, 40.0);
CONSTRAINT FK_category_category FOREIGN KEY (parent_id)
REFERENCES yii_category (id) ON DELETE CASCADE ON UPDATE RESTRICT INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
) TYPE=INNODB; INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 1',NULL); INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 2',NULL); INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 3',NULL); INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 4',1);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 5',1);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 6',5);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 7',5);
CREATE TABLE yii_post_category
(
category_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
PRIMARY KEY (category_id, post_id),
CONSTRAINT FK_yii_post_category_post FOREIGN KEY (post_id)
REFERENCES yii_post (id) ON DELETE CASCADE ON UPDATE RESTRICT,
CONSTRAINT FK_yii_post_category_category FOREIGN KEY (category_id)
REFERENCES yii_category (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_post_category (category_id, post_id) VALUES (1,1);
INSERT INTO yii_post_category (category_id, post_id) VALUES (2,1);
INSERT INTO yii_post_category (category_id, post_id) VALUES (3,1);
INSERT INTO yii_post_category (category_id, post_id) VALUES (4,2);
INSERT INTO yii_post_category (category_id, post_id) VALUES (1,2);
INSERT INTO yii_post_category (category_id, post_id) VALUES (1,3);
CREATE TABLE yii_order
(
key1 INTEGER NOT NULL,
key2 INTEGER NOT NULL,
name VARCHAR(128),
PRIMARY KEY (key1, key2)
) TYPE=INNODB;
INSERT INTO yii_order (key1,key2,name) VALUES (1,2,'order 12');
INSERT INTO yii_order (key1,key2,name) VALUES (1,3,'order 13');
INSERT INTO yii_order (key1,key2,name) VALUES (2,1,'order 21');
INSERT INTO yii_order (key1,key2,name) VALUES (2,2,'order 22');
CREATE TABLE yii_item
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128),
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
CONSTRAINT FK_order_item FOREIGN KEY (col1,col2)
REFERENCES yii_order (key1,key2) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_item (name,col1,col2) VALUES ('item 1',1,2);
INSERT INTO yii_item (name,col1,col2) VALUES ('item 2',1,2);
INSERT INTO yii_item (name,col1,col2) VALUES ('item 3',1,3);
INSERT INTO yii_item (name,col1,col2) VALUES ('item 4',2,2);
INSERT INTO yii_item (name,col1,col2) VALUES ('item 5',2,2);
CREATE TABLE yii_type
(
int_col INT NOT NULL,
int_col2 INTEGER DEFAULT 1,
char_col CHAR(100) NOT NULL,
char_col2 VARCHAR(100) DEFAULT 'something',
char_col3 TEXT,
float_col REAL(4,3) NOT NULL,
float_col2 DOUBLE DEFAULT 1.23,
blob_col BLOB,
numeric_col NUMERIC(5,2) DEFAULT 33.22,
time TIMESTAMP DEFAULT '2002-01-01',
bool_col BOOL NOT NULL,
bool_col2 BOOLEAN DEFAULT 1
) TYPE=INNODB;

4
tests/unit/framework/base/DictionaryTest.php

@ -86,14 +86,14 @@ class DictionaryTest extends \yiiunit\TestCase
public function testFromArray() public function testFromArray()
{ {
$array=array('key3'=>$this->item3,'key4'=>$this->item1); $array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->fromArray($array); $this->dictionary->copyFrom($array);
$this->assertEquals(2, $this->dictionary->getCount()); $this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']); $this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']); $this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->dictionary->fromArray($this); $this->dictionary->copyFrom($this);
} }
public function testMergeWith() public function testMergeWith()

1
tests/unit/framework/base/ObjectTest.php

@ -117,7 +117,6 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertTrue(empty($this->object->Text)); $this->assertTrue(empty($this->object->Text));
} }
public function testEvaluateExpression() public function testEvaluateExpression()
{ {
$object = new NewObject; $object = new NewObject;

4
tests/unit/framework/base/VectorTest.php

@ -113,10 +113,10 @@ class VectorTest extends \yiiunit\TestCase
public function testFromArray() public function testFromArray()
{ {
$array=array($this->item3,$this->item1); $array=array($this->item3,$this->item1);
$this->vector->fromArray($array); $this->vector->copyFrom($array);
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1); $this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->vector->fromArray($this); $this->vector->copyFrom($this);
} }
public function testMergeWith() public function testMergeWith()

100
tests/unit/framework/db/dao/CommandTest.php

@ -18,33 +18,33 @@ class CommandTest extends \yiiunit\MysqlTestCase
$this->assertEquals(null, $command->sql); $this->assertEquals(null, $command->sql);
// string // string
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql); $this->assertEquals($sql, $command->sql);
// Query object // Query object
$query = new Query; $query = new Query;
$query->select('id')->from('tbl_user'); $query->select('id')->from('tbl_customer');
$command = $db->createCommand($query); $command = $db->createCommand($query);
$this->assertEquals("SELECT `id` FROM `tbl_user`", $command->sql); $this->assertEquals("SELECT `id` FROM `tbl_customer`", $command->sql);
// array // array
$command = $db->createCommand(array( $command = $db->createCommand(array(
'select' => 'name', 'select' => 'name',
'from' => 'tbl_user', 'from' => 'tbl_customer',
)); ));
$this->assertEquals("SELECT `name` FROM `tbl_user`", $command->sql); $this->assertEquals("SELECT `name` FROM `tbl_customer`", $command->sql);
} }
function testGetSetSql() function testGetSetSql()
{ {
$db = $this->getConnection(false); $db = $this->getConnection(false);
$sql = 'SELECT * FROM yii_user'; $sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql); $this->assertEquals($sql, $command->sql);
$sql2 = 'SELECT * FROM yii_yii_post'; $sql2 = 'SELECT * FROM tbl_order';
$command->sql = $sql2; $command->sql = $sql2;
$this->assertEquals($sql2, $command->sql); $this->assertEquals($sql2, $command->sql);
} }
@ -53,7 +53,7 @@ class CommandTest extends \yiiunit\MysqlTestCase
{ {
$db = $this->getConnection(false); $db = $this->getConnection(false);
$command = $db->createCommand('SELECT * FROM yii_user'); $command = $db->createCommand('SELECT * FROM tbl_customer');
$this->assertEquals(null, $command->pdoStatement); $this->assertEquals(null, $command->pdoStatement);
$command->prepare(); $command->prepare();
$this->assertNotEquals(null, $command->pdoStatement); $this->assertNotEquals(null, $command->pdoStatement);
@ -65,11 +65,11 @@ class CommandTest extends \yiiunit\MysqlTestCase
{ {
$db = $this->getConnection(); $db = $this->getConnection();
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (\'test comment\', 1, 1)'; $sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals(1, $command->execute()); $this->assertEquals(1, $command->execute());
$sql = 'SELECT COUNT(*) FROM yii_comment WHERE content=\'test comment\''; $sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar()); $this->assertEquals(1, $command->queryScalar());
@ -83,55 +83,55 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection(); $db = $this->getConnection();
// query // query
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$reader = $db->createCommand($sql)->query(); $reader = $db->createCommand($sql)->query();
$this->assertTrue($reader instanceof DataReader); $this->assertTrue($reader instanceof DataReader);
// queryAll // queryAll
$rows = $db->createCommand('SELECT * FROM yii_post')->queryAll(); $rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
$this->assertEquals(5, count($rows)); $this->assertEquals(3, count($rows));
$row = $rows[2]; $row = $rows[2];
$this->assertEquals(3, $row['id']); $this->assertEquals(3, $row['id']);
$this->assertEquals($row['title'], 'post 3'); $this->assertEquals('user3', $row['name']);
$rows = $db->createCommand('SELECT * FROM yii_post WHERE id=10')->queryAll(); $rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
$this->assertEquals(array(), $rows); $this->assertEquals(array(), $rows);
// queryRow // queryRow
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer ORDER BY id';
$row = $db->createCommand($sql)->queryRow(); $row = $db->createCommand($sql)->queryRow();
$this->assertEquals(1, $row['id']); $this->assertEquals(1, $row['id']);
$this->assertEquals('post 1', $row['title'], 'post 1'); $this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->prepare(); $command->prepare();
$row = $command->queryRow(); $row = $command->queryRow();
$this->assertEquals(1, $row['id']); $this->assertEquals(1, $row['id']);
$this->assertEquals('post 1', $row['title']); $this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM yii_post WHERE id=10'; $sql = 'SELECT * FROM tbl_customer WHERE id=10';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertFalse($command->queryRow()); $this->assertFalse($command->queryRow());
// queryColumn // queryColumn
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$column = $db->createCommand($sql)->queryColumn(); $column = $db->createCommand($sql)->queryColumn();
$this->assertEquals(range(1, 5), $column); $this->assertEquals(range(1, 3), $column);
$command = $db->createCommand('SELECT id FROM yii_post WHERE id=10'); $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertEquals(array(), $command->queryColumn()); $this->assertEquals(array(), $command->queryColumn());
// queryScalar // queryScalar
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer ORDER BY id';
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1); $this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
$sql = 'SELECT id FROM yii_post'; $sql = 'SELECT id FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->prepare(); $command->prepare();
$this->assertEquals(1, $command->queryScalar()); $this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT id FROM yii_post WHERE id=10'); $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertFalse($command->queryScalar()); $this->assertFalse($command->queryScalar());
$command = $db->createCommand('bad SQL'); $command = $db->createCommand('bad SQL');
@ -144,20 +144,22 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection(); $db = $this->getConnection();
// bindParam // bindParam
$sql = 'INSERT INTO yii_post(title,create_time,author_id) VALUES (:title, :create_time, 1)'; $sql = 'INSERT INTO tbl_customer(email,name,address) VALUES (:email, :name, :address)';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$title = 'test title'; $email = 'user4@example.com';
$createTime = time(); $name = 'user4';
$command->bindParam(':title', $title); $address = 'address4';
$command->bindParam(':create_time', $createTime); $command->bindParam(':email', $email);
$command->bindParam(':name', $name);
$command->bindParam(':address', $address);
$command->execute(); $command->execute();
$sql = 'SELECT create_time FROM yii_post WHERE title=:title'; $sql = 'SELECT name FROM tbl_customer WHERE email=:email';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->bindParam(':title', $title); $command->bindParam(':email', $email);
$this->assertEquals($createTime, $command->queryScalar()); $this->assertEquals($name, $command->queryScalar());
$sql = 'INSERT INTO yii_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)'; $sql = 'INSERT INTO tbl_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$intCol = 123; $intCol = 123;
$charCol = 'abc'; $charCol = 'abc';
@ -173,7 +175,7 @@ class CommandTest extends \yiiunit\MysqlTestCase
$command->bindParam(':bool_col', $boolCol); $command->bindParam(':bool_col', $boolCol);
$this->assertEquals(1, $command->execute()); $this->assertEquals(1, $command->execute());
$sql = 'SELECT * FROM yii_type'; $sql = 'SELECT * FROM tbl_type';
$row = $db->createCommand($sql)->queryRow(); $row = $db->createCommand($sql)->queryRow();
$this->assertEquals($intCol, $row['int_col']); $this->assertEquals($intCol, $row['int_col']);
$this->assertEquals($charCol, $row['char_col']); $this->assertEquals($charCol, $row['char_col']);
@ -182,23 +184,23 @@ class CommandTest extends \yiiunit\MysqlTestCase
$this->assertEquals($numericCol, $row['numeric_col']); $this->assertEquals($numericCol, $row['numeric_col']);
// bindValue // bindValue
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)'; $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->bindValue(':content', 'test comment'); $command->bindValue(':email', 'user5@example.com');
$command->execute(); $command->execute();
$sql = 'SELECT post_id FROM yii_comment WHERE content=:content'; $sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->bindValue(':content', 'test comment'); $command->bindValue(':name', 'user5');
$this->assertEquals(1, $command->queryScalar()); $this->assertEquals('user5@example.com', $command->queryScalar());
// bind value via query or execute method // bind value via query or execute method
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)'; $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->execute(array(':content' => 'test comment2')); $command->execute(array(':email' => 'user6@example.com'));
$sql = 'SELECT post_id FROM yii_comment WHERE content=:content'; $sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar(array(':content' => 'test comment2'))); $this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5')));
} }
function testFetchMode() function testFetchMode()
@ -206,20 +208,20 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection(); $db = $this->getConnection();
// default: FETCH_ASSOC // default: FETCH_ASSOC
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$result = $command->queryRow(); $result = $command->queryRow();
$this->assertTrue(is_array($result) && isset($result['id'])); $this->assertTrue(is_array($result) && isset($result['id']));
// FETCH_OBJ, customized via fetchMode property // FETCH_OBJ, customized via fetchMode property
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$command->fetchMode = \PDO::FETCH_OBJ; $command->fetchMode = \PDO::FETCH_OBJ;
$result = $command->queryRow(); $result = $command->queryRow();
$this->assertTrue(is_object($result)); $this->assertTrue(is_object($result));
// FETCH_NUM, customized in query method // FETCH_NUM, customized in query method
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$result = $command->queryRow(array(), \PDO::FETCH_NUM); $result = $command->queryRow(array(), \PDO::FETCH_NUM);
$this->assertTrue(is_array($result) && isset($result[0])); $this->assertTrue(is_array($result) && isset($result[0]));

Loading…
Cancel
Save