Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2f37d093
Commit
2f37d093
authored
Dec 31, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
95135f11
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
109 additions
and
85 deletions
+109
-85
Dictionary.php
framework/base/Dictionary.php
+1
-1
Object.php
framework/base/Object.php
+0
-23
Vector.php
framework/base/Vector.php
+1
-1
ActiveQueryBuilder.php
framework/db/ar/ActiveQueryBuilder.php
+22
-0
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+0
-0
TableSchema.php
framework/db/dao/TableSchema.php
+16
-0
Text.php
framework/util/Text.php
+14
-6
mysql.sql
tests/unit/data/mysql.sql
+0
-0
DictionaryTest.php
tests/unit/framework/base/DictionaryTest.php
+2
-2
ObjectTest.php
tests/unit/framework/base/ObjectTest.php
+0
-1
VectorTest.php
tests/unit/framework/base/VectorTest.php
+2
-2
CommandTest.php
tests/unit/framework/db/dao/CommandTest.php
+51
-49
No files found.
framework/base/Dictionary.php
View file @
2f37d093
...
...
@@ -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
())
{
...
...
framework/base/Object.php
View file @
2f37d093
...
...
@@ -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
);
}
}
framework/base/Vector.php
View file @
2f37d093
...
...
@@ -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
)
{
...
...
framework/db/ar/ActiveQueryBuilder.php
0 → 100644
View file @
2f37d093
<?php
/**
* ActiveQueryBuilder class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 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
{
}
\ No newline at end of file
framework/db/ar/ActiveRecord.php
View file @
2f37d093
This diff is collapsed.
Click to expand it.
framework/db/dao/TableSchema.php
View file @
2f37d093
...
...
@@ -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
}
'."
);
}
}
}
}
framework/util/Text.php
View file @
2f37d093
...
...
@@ -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,9 +25,9 @@ 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
(
$rules
=
array
(
'/(x|ch|ss|sh|us|as|is|os)$/i'
=>
'\1es'
,
'/(?:([^f])fe|([lr])f)$/i'
=>
'\1\2ves'
,
'/(m)an$/i'
=>
'\1en'
,
...
...
@@ -33,11 +35,17 @@ class Text
'/(r)y$/i'
=>
'\1ies'
,
'/s$/'
=>
's'
,
);
foreach
(
$rules
as
$rule
=>
$replacement
)
foreach
(
$rules
as
$rule
=>
$replacement
)
{
if
(
preg_match
(
$rule
,
$name
))
return
preg_replace
(
$rule
,
$replacement
,
$name
);
if
(
preg_match
(
$rule
,
$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
))));
}
}
tests/unit/data/mysql.sql
View file @
2f37d093
This diff is collapsed.
Click to expand it.
tests/unit/framework/base/DictionaryTest.php
View file @
2f37d093
...
...
@@ -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
()
...
...
tests/unit/framework/base/ObjectTest.php
View file @
2f37d093
...
...
@@ -117,7 +117,6 @@ class ObjectTest extends \yiiunit\TestCase
$this
->
assertTrue
(
empty
(
$this
->
object
->
Text
));
}
public
function
testEvaluateExpression
()
{
$object
=
new
NewObject
;
...
...
tests/unit/framework/base/VectorTest.php
View file @
2f37d093
...
...
@@ -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
()
...
...
tests/unit/framework/db/dao/CommandTest.php
View file @
2f37d093
...
...
@@ -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_
us
er'
);
$query
->
select
(
'id'
)
->
from
(
'tbl_
custom
er'
);
$command
=
$db
->
createCommand
(
$query
);
$this
->
assertEquals
(
"SELECT `id` FROM `tbl_
us
er`"
,
$command
->
sql
);
$this
->
assertEquals
(
"SELECT `id` FROM `tbl_
custom
er`"
,
$command
->
sql
);
// array
$command
=
$db
->
createCommand
(
array
(
'select'
=>
'name'
,
'from'
=>
'tbl_
us
er'
,
'from'
=>
'tbl_
custom
er'
,
));
$this
->
assertEquals
(
"SELECT `name` FROM `tbl_
us
er`"
,
$command
->
sql
);
$this
->
assertEquals
(
"SELECT `name` FROM `tbl_
custom
er`"
,
$command
->
sql
);
}
function
testGetSetSql
()
{
$db
=
$this
->
getConnection
(
false
);
$sql
=
'SELECT * FROM
yii_us
er'
;
$sql
=
'SELECT * FROM
tbl_custom
er'
;
$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_us
er'
);
$command
=
$db
->
createCommand
(
'SELECT * FROM
tbl_custom
er'
);
$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
[
'titl
e'
]);
$this
->
assertEquals
(
'
user1'
,
$row
[
'nam
e'
]);
$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
(
$
createTi
me
,
$command
->
queryScalar
());
$command
->
bindParam
(
':
email'
,
$email
);
$this
->
assertEquals
(
$
na
me
,
$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
]));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment