Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
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
without authentication
meibuyu-micro
Commits
cf24e168
Commit
cf24e168
authored
Feb 12, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化repository文件
parent
1652c299
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
33 deletions
+26
-33
RepositoryInterface.php
src/Repository/Contracts/RepositoryInterface.php
+6
-18
BaseRepository.php
src/Repository/Eloquent/BaseRepository.php
+20
-15
No files found.
src/Repository/Contracts/RepositoryInterface.php
View file @
cf24e168
...
@@ -13,6 +13,12 @@ interface RepositoryInterface
...
@@ -13,6 +13,12 @@ interface RepositoryInterface
{
{
public
function
list
();
public
function
list
();
public
function
show
(
$id
);
/**
* @param array $columns
* @return mixed
*/
public
function
all
(
$columns
=
array
(
'*'
));
public
function
all
(
$columns
=
array
(
'*'
));
public
function
paginate
(
$perPage
=
10
,
$columns
=
array
(
'*'
));
public
function
paginate
(
$perPage
=
10
,
$columns
=
array
(
'*'
));
...
@@ -27,22 +33,4 @@ interface RepositoryInterface
...
@@ -27,22 +33,4 @@ interface RepositoryInterface
public
function
findBy
(
$field
,
$value
,
$columns
=
array
(
'*'
));
public
function
findBy
(
$field
,
$value
,
$columns
=
array
(
'*'
));
/**
* Load relations
*
* @param $relations
*
* @return $this
*/
public
function
with
(
$relations
);
/**
* Order collection by a given column
*
* @param string $column
* @param string $direction
*
* @return $this
*/
public
function
orderBy
(
$column
,
$direction
=
'asc'
);
}
}
\ No newline at end of file
src/Repository/Eloquent/BaseRepository.php
View file @
cf24e168
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
namespace
Meibuyu\Micro\Repository\Eloquent
;
namespace
Meibuyu\Micro\Repository\Eloquent
;
use
Hyperf\DbConnection\Model\Model
;
use
Hyperf\DbConnection\Model\Model
;
use
Hyperf\HttpServer\Contract\RequestInterface
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\RepositoryException
;
use
Meibuyu\Micro\Exceptions\RepositoryException
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
...
@@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface
*/
*/
private
$container
;
private
$container
;
/**
* @var RequestInterface
*/
protected
$request
;
/**
/**
* @var Model
* @var Model
*/
*/
...
@@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface
/**
/**
* BaseRepository constructor.
* BaseRepository constructor.
* @param ContainerInterface $container
* @param ContainerInterface $container
* @param RequestInterface $request
* @throws RepositoryException
* @throws RepositoryException
*/
*/
public
function
__construct
(
ContainerInterface
$container
)
public
function
__construct
(
ContainerInterface
$container
,
RequestInterface
$request
)
{
{
$this
->
container
=
$container
;
$this
->
container
=
$container
;
$this
->
request
=
$request
;
$this
->
makeModel
();
$this
->
makeModel
();
$this
->
makeValidator
();
$this
->
makeValidator
();
}
}
...
@@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface
/**
/**
* @param $id
* @param $id
* @param array $columns
* @param array $columns
* @return mixed
* @return mixed
|Model
* @throws HttpResponseException
* @throws HttpResponseException
*/
*/
public
function
find
(
$id
,
$columns
=
[
'*'
])
public
function
find
(
$id
,
$columns
=
[
'*'
])
...
@@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface
public
function
list
()
public
function
list
()
{
{
return
$this
->
model
->
all
();
return
$this
->
all
();
}
}
public
function
with
(
$relations
)
public
function
show
(
$id
)
{
{
$this
->
model
=
$this
->
model
->
with
(
$relations
);
return
$this
->
find
(
$id
);
return
$this
;
}
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
{
$this
->
model
=
$this
->
model
->
orderBy
(
$column
,
$direction
);
return
$this
;
}
}
public
function
paginate
(
$perPage
=
10
,
$columns
=
[
'*'
])
public
function
paginate
(
$perPage
=
10
,
$columns
=
[
'*'
])
...
@@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface
* @param array $attributes
* @param array $attributes
* @param $id
* @param $id
* @return Model | mixed
* @return Model | mixed
* @throws HttpResponseException
* @throws ValidatorException
* @throws ValidatorException
*/
*/
public
function
update
(
array
$attributes
,
$id
)
public
function
update
(
array
$attributes
,
$id
)
...
@@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface
$this
->
validator
->
with
(
$attributes
)
->
setId
(
$id
)
->
passesOrFail
(
ValidatorInterface
::
RULE_UPDATE
);
$this
->
validator
->
with
(
$attributes
)
->
setId
(
$id
)
->
passesOrFail
(
ValidatorInterface
::
RULE_UPDATE
);
}
}
$model
=
$this
->
model
->
findOrFail
(
$id
);
$model
=
$this
->
find
(
$id
);
$model
->
fill
(
$attributes
);
$model
->
fill
(
$attributes
);
$model
->
save
();
$model
->
save
();
return
$model
;
return
$model
;
...
@@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface
...
@@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface
public
function
delete
(
$id
)
public
function
delete
(
$id
)
{
{
$model
=
$this
->
find
(
$id
);
$model
=
$this
->
find
(
$id
);
$delete
=
$model
->
delete
();
try
{
$delete
=
$model
->
delete
();
}
catch
(
\Exception
$e
)
{
throw
new
HttpResponseException
(
$e
->
getMessage
());
}
if
(
$delete
!==
false
)
{
if
(
$delete
!==
false
)
{
return
$delete
;
return
$delete
;
}
else
{
}
else
{
...
...
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