Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
PHP
laravel-repository
Commits
c7cc5e97
Commit
c7cc5e97
authored
4 years ago
by
康帅杰
Browse files
Options
Download
Email Patches
Plain Diff
增加数据库事务
parent
a9aa1e3b
master
1.0.5
1.0.4
1.0.3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
src/DBTrait.php
+33
-0
src/DBTrait.php
src/Repository.php
+2
-1
src/Repository.php
tests/Model.php
+19
-0
tests/Model.php
tests/RepositoryTest.php
+59
-3
tests/RepositoryTest.php
tests/TestCase.php
+9
-1
tests/TestCase.php
tests/migration.php
+31
-0
tests/migration.php
with
153 additions
and
5 deletions
+153
-5
src/DBTrait.php
0 → 100644
View file @
c7cc5e97
<?php
/**
* @Author: kidkang
* @Date: 2021-03-08 11:34:36
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-08 11:36:28
*/
namespace
Yjtec\Repo
;
use
DB
;
trait
DBTrait
{
public
function
start
()
{
DB
::
beginTransaction
();
}
public
function
commit
()
{
DB
::
commit
();
}
public
function
rollback
()
{
DB
::
rollBack
();
}
public
function
transaction
(
$callback
)
{
DB
::
transaction
(
$callback
);
}
}
This diff is collapsed.
Click to expand it.
src/Repository.php
View file @
c7cc5e97
...
...
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
abstract
class
Repository
{
use
DBTrait
;
protected
$app
;
//App容器
protected
$model
;
//操作的model
public
$ErrorCode
;
//错误码
...
...
@@ -45,7 +46,7 @@ abstract class Repository
return
$this
->
model
->
paginate
(
$page
);
}
public
function
list
(
$where
){
function
list
(
$where
)
{
return
$this
->
model
->
where
(
$where
)
->
get
();
}
public
function
add
(
$data
)
...
...
This diff is collapsed.
Click to expand it.
tests/Model.php
0 → 100644
View file @
c7cc5e97
<?php
/**
* @Author: kidkang
* @Date: 2021-03-08 11:27:55
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-08 13:37:17
*/
namespace
Yjtec\Repo\Test
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
as
BaseModel
;
class
Model
extends
BaseModel
{
use
HasFactory
;
protected
$table
=
'demo'
;
protected
$fillable
=
[
'name'
];
}
This diff is collapsed.
Click to expand it.
tests/RepositoryTest.php
View file @
c7cc5e97
...
...
@@ -4,15 +4,71 @@
* @Author: kidkang
* @Date: 2021-03-02 18:04:27
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-0
2
1
8:05:21
* @Last Modified time: 2021-03-0
8
1
3:47:45
*/
namespace
Yjtec\Repo\Test
;
use
Yjtec\Repo\Repository
;
class
RepositoryTest
extends
TestCase
{
public
function
testExample
()
public
function
setUp
():
void
{
parent
::
setUp
();
}
public
function
testDBevent
()
{
$repo
=
$this
->
getRepo
();
$repo
->
start
();
$repo
->
commit
();
$repo
->
rollback
();
$this
->
assertTrue
(
true
);
}
public
function
testTransaction
()
{
$repo
=
$this
->
getRepo
();
$repo
->
transaction
(
function
()
{
Model
::
create
([
'name'
=>
'foo'
,
]);
});
$a
=
Model
::
where
(
'name'
,
'foo'
)
->
first
();
$this
->
assertEquals
(
'foo'
,
$a
->
name
);
}
public
function
testTransactionWithException
()
{
$repo
=
$this
->
getRepo
();
$this
->
expectException
(
\
Exception
::
class
);
$repo
->
transaction
(
function
()
use
(
$repo
)
{
Model
::
create
([
'name'
=>
'bar'
,
]);
$repo
->
throw
();
});
}
protected
function
getRepo
()
{
return
new
RepositoryDemo
(
$this
->
app
);
}
}
class
RepositoryDemo
extends
Repository
{
/**
* Specify Model class name
*
* @return string
*/
public
function
model
()
{
// TODO: Implement model() method.
return
Model
::
class
;
}
function
throw
()
{
throw
new
\
Exception
(
'error'
);
}
}
This diff is collapsed.
Click to expand it.
tests/TestCase.php
View file @
c7cc5e97
...
...
@@ -4,7 +4,7 @@
* @Author: kidkang
* @Date: 2021-03-02 18:01:31
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-0
7
1
0:00:03
* @Last Modified time: 2021-03-0
8
1
3:36:49
*/
namespace
Yjtec\Repo\Test
;
...
...
@@ -36,7 +36,15 @@ abstract class TestCase extends Orchestra
*/
protected
function
getEnvironmentSetUp
(
$app
)
{
$app
[
'config'
]
->
set
(
'database.default'
,
'sqlite'
);
$app
[
'config'
]
->
set
(
'database.connections.sqlite'
,
[
'driver'
=>
'sqlite'
,
'database'
=>
':memory:'
,
'prefix'
=>
''
,
]);
$migraiont
=
include_once
__DIR__
.
'/migration.php'
;
(
new
\
CreateDemosTable
)
->
up
();
}
}
This diff is collapsed.
Click to expand it.
tests/migration.php
0 → 100644
View file @
c7cc5e97
<?php
use
Illuminate\Database\Migrations\Migration
;
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Support\Facades\Schema
;
class
CreateDemosTable
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up
()
{
Schema
::
create
(
'demo'
,
function
(
Blueprint
$table
)
{
$table
->
string
(
'name'
);
$table
->
timestamps
();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down
()
{
Schema
::
dropIfExists
(
'demo'
);
}
}
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help