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
46ab456f
Commit
46ab456f
authored
Jan 12, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished new AR.
parent
cf6c73c6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
95 additions
and
2 deletions
+95
-2
ActiveRecord.php
framework/db/ActiveRecord.php
+0
-0
Customer.php
tests/unit/data/ar/Customer.php
+1
-1
Order.php
tests/unit/data/ar/Order.php
+11
-0
mysql.sql
tests/unit/data/mysql.sql
+2
-1
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+81
-0
No files found.
framework/db/ActiveRecord.php
View file @
46ab456f
This diff is collapsed.
Click to expand it.
tests/unit/data/ar/Customer.php
View file @
46ab456f
...
@@ -15,7 +15,7 @@ class Customer extends ActiveRecord
...
@@ -15,7 +15,7 @@ class Customer extends ActiveRecord
public
function
getOrders
()
public
function
getOrders
()
{
{
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
));
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
))
->
orderBy
(
'id'
)
;
}
}
/**
/**
...
...
tests/unit/data/ar/Order.php
View file @
46ab456f
...
@@ -33,4 +33,14 @@ class Order extends ActiveRecord
...
@@ -33,4 +33,14 @@ class Order extends ActiveRecord
->
viaTable
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
->
viaTable
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
->
where
(
array
(
'category_id'
=>
1
));
->
where
(
array
(
'category_id'
=>
1
));
}
}
public
function
beforeSave
(
$insert
)
{
if
(
parent
::
beforeSave
(
$insert
))
{
$this
->
create_time
=
time
();
return
true
;
}
else
{
return
false
;
}
}
}
}
\ No newline at end of file
tests/unit/data/mysql.sql
View file @
46ab456f
...
@@ -43,7 +43,8 @@ CREATE TABLE `tbl_order` (
...
@@ -43,7 +43,8 @@ CREATE TABLE `tbl_order` (
`customer_id`
int
(
11
)
NOT
NULL
,
`customer_id`
int
(
11
)
NOT
NULL
,
`create_time`
int
(
11
)
NOT
NULL
,
`create_time`
int
(
11
)
NOT
NULL
,
`total`
decimal
(
10
,
0
)
NOT
NULL
,
`total`
decimal
(
10
,
0
)
NOT
NULL
,
PRIMARY
KEY
(
`id`
)
PRIMARY
KEY
(
`id`
),
CONSTRAINT
`FK_order_customer_id`
FOREIGN
KEY
(
`customer_id`
)
REFERENCES
`tbl_customer`
(
`id`
)
ON
DELETE
CASCADE
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
CREATE
TABLE
`tbl_order_item`
(
CREATE
TABLE
`tbl_order_item`
(
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
46ab456f
...
@@ -8,6 +8,7 @@ use yiiunit\data\ar\ActiveRecord;
...
@@ -8,6 +8,7 @@ use yiiunit\data\ar\ActiveRecord;
use
yiiunit\data\ar\Customer
;
use
yiiunit\data\ar\Customer
;
use
yiiunit\data\ar\OrderItem
;
use
yiiunit\data\ar\OrderItem
;
use
yiiunit\data\ar\Order
;
use
yiiunit\data\ar\Order
;
use
yiiunit\data\ar\Item
;
class
ActiveRecordTest
extends
\yiiunit\MysqlTestCase
class
ActiveRecordTest
extends
\yiiunit\MysqlTestCase
{
{
...
@@ -190,6 +191,86 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -190,6 +191,86 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
}
}
public
function
testLink
()
{
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
// has many
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
->
link
(
'orders'
,
$order
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
orders
));
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
getOrders
()
->
all
()));
$this
->
assertEquals
(
2
,
$order
->
customer_id
);
// belongs to
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
=
Customer
::
find
(
1
);
$this
->
assertNull
(
$order
->
customer
);
$order
->
link
(
'customer'
,
$customer
);
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
1
,
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
$order
->
customer
->
id
);
// via table
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
0
,
count
(
$order
->
books
));
$orderItem
=
OrderItem
::
find
(
array
(
'order_id'
=>
2
,
'item_id'
=>
1
));
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
1
);
$order
->
link
(
'books'
,
$item
,
array
(
'quantity'
=>
10
,
'subtotal'
=>
100
));
$this
->
assertEquals
(
1
,
count
(
$order
->
books
));
$orderItem
=
OrderItem
::
find
(
array
(
'order_id'
=>
2
,
'item_id'
=>
1
));
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
// via model
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
(
array
(
'order_id'
=>
1
,
'item_id'
=>
3
));
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
3
);
$order
->
link
(
'items'
,
$item
,
array
(
'quantity'
=>
10
,
'subtotal'
=>
100
));
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
(
array
(
'order_id'
=>
1
,
'item_id'
=>
3
));
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
}
public
function
testUnlink
()
{
// has many
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$customer
->
unlink
(
'orders'
,
$customer
->
orders
[
1
]);
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
Order
::
find
(
3
));
// via model
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$order
->
unlink
(
'items'
,
$order
->
items
[
2
]);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
// via table
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
books
));
$order
->
unlink
(
'books'
,
$order
->
books
[
1
]);
$this
->
assertEquals
(
1
,
count
(
$order
->
books
));
$this
->
assertEquals
(
1
,
count
(
$order
->
orderItems
));
}
// public function testInsert()
// public function testInsert()
// {
// {
// $customer = new Customer;
// $customer = new Customer;
...
...
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