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
1b151818
Commit
1b151818
authored
Jan 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed AR bugs.
parent
a7ecbfee
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
107 deletions
+107
-107
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+1
-0
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+2
-2
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+0
-1
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+104
-104
No files found.
framework/db/ar/ActiveQuery.php
View file @
1b151818
...
@@ -235,6 +235,7 @@ class ActiveQuery extends BaseQuery
...
@@ -235,6 +235,7 @@ class ActiveQuery extends BaseQuery
if
(
$relation
->
via
!==
null
)
{
if
(
$relation
->
via
!==
null
)
{
$viaName
=
$relation
->
via
;
$viaName
=
$relation
->
via
;
$viaQuery
=
$primaryModel
->
$viaName
();
$viaQuery
=
$primaryModel
->
$viaName
();
$viaQuery
->
primaryModel
=
null
;
$relation
->
findWith
(
$name
,
$models
,
$viaQuery
);
$relation
->
findWith
(
$name
,
$models
,
$viaQuery
);
}
else
{
}
else
{
$relation
->
findWith
(
$name
,
$models
);
$relation
->
findWith
(
$name
,
$models
);
...
...
framework/db/ar/ActiveRecord.php
View file @
1b151818
...
@@ -382,8 +382,8 @@ abstract class ActiveRecord extends Model
...
@@ -382,8 +382,8 @@ abstract class ActiveRecord extends Model
{
{
if
(
strpos
(
$class
,
'\\'
)
===
false
)
{
if
(
strpos
(
$class
,
'\\'
)
===
false
)
{
$primaryClass
=
get_class
(
$this
);
$primaryClass
=
get_class
(
$this
);
if
(
strpos
(
$primaryClass
,
'\\'
)
!==
false
)
{
if
(
(
$pos
=
strrpos
(
$primaryClass
,
'\\'
)
)
!==
false
)
{
$class
=
dirname
(
$primaryClass
)
.
'\\'
.
$class
;
$class
=
substr
(
$primaryClass
,
0
,
$pos
+
1
)
.
$class
;
}
}
}
}
...
...
framework/db/dao/QueryBuilder.php
View file @
1b151818
...
@@ -588,7 +588,6 @@ class QueryBuilder extends \yii\base\Object
...
@@ -588,7 +588,6 @@ class QueryBuilder extends \yii\base\Object
}
}
}
}
}
}
if
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
strpos
(
$column
,
'('
)
===
false
)
{
$column
=
$this
->
quoteColumnName
(
$column
);
$column
=
$this
->
quoteColumnName
(
$column
);
}
}
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
1b151818
...
@@ -16,110 +16,110 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -16,110 +16,110 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
ActiveRecord
::
$db
=
$this
->
getConnection
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
}
public
function
testFind
()
//
public function testFind()
{
//
{
// find one
//
// find one
$result
=
Customer
::
find
();
//
$result = Customer::find();
$this
->
assertTrue
(
$result
instanceof
ActiveQuery
);
//
$this->assertTrue($result instanceof ActiveQuery);
$customer
=
$result
->
one
();
//
$customer = $result->one();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
//
// find all
//
// find all
$result
=
Customer
::
find
();
//
$result = Customer::find();
$customers
=
$result
->
all
();
//
$customers = $result->all();
$this
->
assertEquals
(
3
,
count
(
$customers
));
//
$this->assertEquals(3, count($customers));
$this
->
assertTrue
(
$customers
[
0
]
instanceof
Customer
);
//
$this->assertTrue($customers[0] instanceof Customer);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
//
$this->assertTrue($customers[1] instanceof Customer);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
//
$this->assertTrue($customers[2] instanceof Customer);
//
// find by a single primary key
//
// find by a single primary key
$customer
=
Customer
::
find
(
2
);
//
$customer = Customer::find(2);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
//
$this->assertEquals('user2', $customer->name);
//
// find by attributes
//
// find by attributes
$customer
=
Customer
::
find
()
->
where
(
array
(
'name'
=>
'user2'
))
->
one
();
//
$customer = Customer::find()->where(array('name' => 'user2'))->one();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
$this
->
assertEquals
(
2
,
$customer
->
id
);
//
$this->assertEquals(2, $customer->id);
//
// find by Query array
//
// find by Query array
$query
=
array
(
//
$query = array(
'where'
=>
'id=:id'
,
//
'where' => 'id=:id',
'params'
=>
array
(
':id'
=>
2
),
//
'params' => array(':id' => 2),
);
//
);
$customer
=
Customer
::
find
(
$query
)
->
one
();
//
$customer = Customer::find($query)->one();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
//
$this->assertEquals('user2', $customer->name);
//
// find count
//
// find count
$this
->
assertEquals
(
3
,
Customer
::
count
()
->
value
());
//
$this->assertEquals(3, Customer::count()->value());
$this
->
assertEquals
(
2
,
Customer
::
count
(
array
(
//
$this->assertEquals(2, Customer::count(array(
'where'
=>
'id=1 OR id=2'
,
//
'where' => 'id=1 OR id=2',
))
->
value
());
//
))->value());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
select
(
'COUNT(*)'
)
->
where
(
'id=1 OR id=2'
)
->
value
());
//
$this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value());
}
//
}
//
public
function
testFindBySql
()
//
public function testFindBySql()
{
//
{
// find one
//
// find one
$customer
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer ORDER BY id DESC'
)
->
one
();
//
$customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
//
$this->assertEquals('user3', $customer->name);
//
// find all
//
// find all
$customers
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer'
)
->
all
();
//
$customers = Customer::findBySql('SELECT * FROM tbl_customer')->all();
$this
->
assertEquals
(
3
,
count
(
$customers
));
//
$this->assertEquals(3, count($customers));
//
// find with parameter binding
//
// find with parameter binding
$customer
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer WHERE id=:id'
,
array
(
':id'
=>
2
))
->
one
();
//
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
//
$this->assertTrue($customer instanceof Customer);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
//
$this->assertEquals('user2', $customer->name);
}
//
}
//
public
function
testScope
()
//
public function testScope()
{
//
{
$customers
=
Customer
::
find
(
array
(
//
$customers = Customer::find(array(
'scopes'
=>
array
(
'active'
),
//
'scopes' => array('active'),
))
->
all
();
//
))->all();
$this
->
assertEquals
(
2
,
count
(
$customers
));
//
$this->assertEquals(2, count($customers));
//
$customers
=
Customer
::
find
()
->
active
()
->
all
();
//
$customers = Customer::find()->active()->all();
$this
->
assertEquals
(
2
,
count
(
$customers
));
//
$this->assertEquals(2, count($customers));
}
//
}
//
public
function
testFindLazy
()
//
public function testFindLazy()
{
//
{
/** @var $customer Customer */
//
/** @var $customer Customer */
$customer
=
Customer
::
find
(
2
);
//
$customer = Customer::find(2);
$orders
=
$customer
->
orders
;
//
$orders = $customer->orders;
$this
->
assertEquals
(
2
,
count
(
$orders
));
//
$this->assertEquals(2, count($orders));
//
$orders
=
$customer
->
orders
()
->
where
(
'id=3'
)
->
all
();
//
$orders = $customer->orders()->where('id=3')->all();
$this
->
assertEquals
(
1
,
count
(
$orders
));
//
$this->assertEquals(1, count($orders));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
//
$this->assertEquals(3, $orders[0]->id);
}
//
}
//
public
function
testFindEager
()
//
public function testFindEager()
{
//
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
)
->
all
();
//
$customers = Customer::find()->with('orders')->all();
$this
->
assertEquals
(
3
,
count
(
$customers
));
//
$this->assertEquals(3, count($customers));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
//
$this->assertEquals(1, count($customers[0]->orders));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
//
$this->assertEquals(2, count($customers[1]->orders));
}
//
}
//
public
function
testFindLazyVia
()
//
public function testFindLazyVia()
{
//
{
/** @var $order Order */
//
/** @var $order Order */
$order
=
Order
::
find
(
1
);
//
$order = Order::find(1);
$this
->
assertEquals
(
1
,
$order
->
id
);
//
$this->assertEquals(1, $order->id);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
//
$this->assertEquals(2, count($order->items));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
//
$this->assertEquals(1, $order->items[0]->id);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
//
$this->assertEquals(2, $order->items[1]->id);
//
$order
=
Order
::
find
(
1
);
//
$order = Order::find(1);
$order
->
id
=
100
;
//
$order->id = 100;
$this
->
assertEquals
(
array
(),
$order
->
items
);
//
$this->assertEquals(array(), $order->items);
}
//
}
public
function
testFindEagerVia
()
public
function
testFindEagerVia
()
{
{
...
...
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