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. 1772
      framework/db/ar/ActiveRecord.php
  6. 16
      framework/db/dao/TableSchema.php
  7. 12
      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`
* @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 ($this->_d !== array()) {

23
framework/base/Object.php

@ -342,27 +342,4 @@ class 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`
* @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 ($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
{
}

1772
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;
use yii\db\Exception;
/**
* TableSchema represents the metadata of a database table.
*
@ -82,4 +84,18 @@ class TableSchema extends \yii\base\Object
{
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}'.");
}
}
}
}

12
framework/util/Text.php

@ -14,6 +14,8 @@ namespace yii\util;
/**
* Text helper
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class Text
@ -23,7 +25,7 @@ class Text
* @param string $name the word to be pluralized
* @return string the pluralized word
*/
public function pluralize($name)
public static function pluralize($name)
{
$rules = array(
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
@ -35,9 +37,15 @@ class Text
);
foreach ($rules as $rule => $replacement)
{
if(preg_match($rule,$name))
if (preg_match($rule, $name)) {
return preg_replace($rule, $replacement, $name);
}
}
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.
* To test this feature, you need to create a database named 'yii' on 'localhost'
* and create an account 'test/test' which owns this test database.
* This is the database schema for testing MySQL support of Yii DAO and Active Record.
* The following database setup is required to perform then relevant tests:
* Database name: yiitest
* username: test
* password: test
* charset: utf8
*/
DROP TABLE IF EXISTS yii_type CASCADE;
DROP TABLE IF EXISTS yii_item CASCADE;
DROP TABLE IF EXISTS yii_order CASCADE;
DROP TABLE IF EXISTS yii_post_category CASCADE;
DROP TABLE IF EXISTS yii_category CASCADE;
DROP TABLE IF EXISTS yii_comment CASCADE;
DROP TABLE IF EXISTS yii_post CASCADE;
DROP TABLE IF EXISTS yii_profile CASCADE;
DROP TABLE IF EXISTS yii_user CASCADE;
CREATE TABLE yii_user
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(128) NOT NULL,
password VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL
) TYPE=INNODB;
INSERT INTO yii_user (username, password, email) VALUES ('user1','pass1','email1');
INSERT INTO yii_user (username, password, email) VALUES ('user2','pass2','email2');
INSERT INTO yii_user (username, password, email) VALUES ('user3','pass3','email3');
CREATE TABLE yii_profile
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(128) NOT NULL,
last_name VARCHAR(128) NOT NULL,
user_id INTEGER NOT NULL,
CONSTRAINT FK_profile_user FOREIGN KEY (user_id)
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_profile (first_name, last_name, user_id) VALUES ('first 1','last 1',1);
INSERT INTO yii_profile (first_name, last_name, user_id) VALUES ('first 2','last 2',2);
CREATE TABLE yii_post
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(128) NOT NULL,
create_time INTEGER NOT NULL,
author_id INTEGER NOT NULL,
content TEXT,
CONSTRAINT FK_post_author FOREIGN KEY (author_id)
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 1','1324854194',1,'content 1');
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 2','1324855194',2,'content 2');
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');
INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 5','1324858194',3,'content 5');
CREATE TABLE yii_comment
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
content TEXT NOT NULL,
post_id INTEGER NOT NULL,
author_id INTEGER NOT NULL,
CONSTRAINT FK_post_comment FOREIGN KEY (post_id)
REFERENCES yii_post (id) ON DELETE CASCADE ON UPDATE RESTRICT,
CONSTRAINT FK_user_comment FOREIGN KEY (author_id)
REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 1',1, 2);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 2',1, 2);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 3',1, 2);
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 yii_comment (content, post_id, author_id) VALUES ('comment 6',3, 2);
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 yii_comment (content, post_id, author_id) VALUES ('comment 9',3, 2);
INSERT INTO yii_comment (content, post_id, author_id) VALUES ('comment 10',5, 3);
CREATE TABLE yii_category
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
parent_id INTEGER,
CONSTRAINT FK_category_category FOREIGN KEY (parent_id)
REFERENCES yii_category (id) ON DELETE CASCADE ON UPDATE RESTRICT
) TYPE=INNODB;
INSERT INTO yii_category (name, parent_id) VALUES ('cat 1',NULL);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 2',NULL);
INSERT INTO yii_category (name, parent_id) VALUES ('cat 3',NULL);
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;
DROP TABLE IF EXISTS tbl_order_item CASCADE;
DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS tbl_order CASCADE;
DROP TABLE IF EXISTS tbl_category CASCADE;
DROP TABLE IF EXISTS tbl_customer CASCADE;
DROP TABLE IF EXISTS tbl_type CASCADE;
CREATE TABLE `tbl_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL,
`address` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`category_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_item_category_id` (`category_id`),
CONSTRAINT `FK_item_category_id` FOREIGN KEY (`category_id`) REFERENCES `tbl_category` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`create_time` int(11) NOT NULL,
`total` decimal(10,0) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_order_item` (
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`subtotal` decimal(10,0) NOT NULL,
PRIMARY KEY (`order_id`,`item_id`),
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,
CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `tbl_item` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_type` (
`int_col` int(11) NOT NULL,
`int_col2` int(11) DEFAULT '1',
`char_col` char(100) NOT NULL,
`char_col2` varchar(100) DEFAULT 'something',
`char_col3` text,
`float_col` double(4,3) NOT NULL,
`float_col2` double DEFAULT '1.23',
`blob_col` blob,
`numeric_col` decimal(5,2) DEFAULT '33.22',
`time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
`bool_col` tinyint(1) NOT NULL,
`bool_col2` tinyint(1) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO tbl_customer (email, name, address) VALUES ('user1@example.com', 'user1', 'address1');
INSERT INTO tbl_customer (email, name, address) VALUES ('user2@example.com', 'user2', 'address2');
INSERT INTO tbl_customer (email, name, address) VALUES ('user3@example.com', 'user3', 'address3');
INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies');
INSERT INTO tbl_item (name, category_id) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO tbl_item (name, category_id) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO tbl_item (name, category_id) VALUES ('Ice Age', 2);
INSERT INTO tbl_item (name, category_id) VALUES ('Toy Story', 2);
INSERT INTO tbl_item (name, category_id) VALUES ('Cars', 2);
INSERT INTO tbl_order (customer_id, create_time, total) VALUES (1, 1325282384, 110.0);
INSERT INTO tbl_order (customer_id, create_time, total) VALUES (2, 1325334482, 33.0);
INSERT INTO tbl_order (customer_id, create_time, total) VALUES (3, 1325502201, 40.0);
INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
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 tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);

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

@ -86,14 +86,14 @@ class DictionaryTest extends \yiiunit\TestCase
public function testFromArray()
{
$array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->fromArray($array);
$this->dictionary->copyFrom($array);
$this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception');
$this->dictionary->fromArray($this);
$this->dictionary->copyFrom($this);
}
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));
}
public function testEvaluateExpression()
{
$object = new NewObject;

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

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

Loading…
Cancel
Save