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
66b698fb
Commit
66b698fb
authored
Feb 10, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加with和orderby方法,新增和更新加入验证器验证
parent
8ae55d25
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
5 deletions
+60
-5
RepositoryInterface.php
src/Repository/Contracts/RepositoryInterface.php
+19
-0
BaseRepository.php
src/Repository/Eloquent/BaseRepository.php
+41
-5
No files found.
src/Repository/Contracts/RepositoryInterface.php
View file @
66b698fb
...
...
@@ -24,4 +24,23 @@ interface RepositoryInterface
public
function
find
(
$id
,
$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 @
66b698fb
...
...
@@ -12,6 +12,7 @@ namespace Meibuyu\Micro\Repository\Eloquent;
use
Hyperf\DbConnection\Model\Model
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\RepositoryException
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
use
Meibuyu\Micro\Repository\Contracts\RepositoryInterface
;
use
Meibuyu\Micro\Validator\Contracts\ValidatorInterface
;
use
Psr\Container\ContainerInterface
;
...
...
@@ -96,7 +97,6 @@ abstract class BaseRepository implements RepositoryInterface
return
null
;
}
/**
* @param $id
* @param array $columns
...
...
@@ -121,20 +121,52 @@ abstract class BaseRepository implements RepositoryInterface
return
$this
->
model
->
get
(
$columns
);
}
public
function
with
(
$relations
)
{
$this
->
model
=
$this
->
model
->
with
(
$relations
);
return
$this
;
}
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
{
$this
->
model
=
$this
->
model
->
orderBy
(
$column
,
$direction
);
return
$this
;
}
public
function
paginate
(
$perPage
=
10
,
$columns
=
[
'*'
])
{
return
$this
->
model
->
paginate
(
$perPage
,
$columns
);
}
/**
* @param array $attributes
* @return Model
* @throws ValidatorException
*/
public
function
create
(
array
$attributes
)
{
if
(
!
is_null
(
$this
->
validator
))
{
$this
->
validator
->
with
(
$attributes
)
->
passesOrFail
(
ValidatorInterface
::
RULE_CREATE
);
}
$model
=
$this
->
model
->
newInstance
(
$attributes
);
$model
->
save
();
return
$model
;
}
/**
* @param array $attributes
* @param $id
* @return Model | mixed
* @throws ValidatorException
*/
public
function
update
(
array
$attributes
,
$id
)
{
if
(
!
is_null
(
$this
->
validator
))
{
$this
->
validator
->
with
(
$attributes
)
->
setId
(
$id
)
->
passesOrFail
(
ValidatorInterface
::
RULE_UPDATE
);
}
$model
=
$this
->
model
->
findOrFail
(
$id
);
$model
->
fill
(
$attributes
);
$model
->
save
();
...
...
@@ -143,13 +175,18 @@ abstract class BaseRepository implements RepositoryInterface
/**
* @param $id
* @return bool|
null
* @throws
\
Exception
* @return bool|
mixed
* @throws
HttpResponse
Exception
*/
public
function
delete
(
$id
)
{
$model
=
$this
->
find
(
$id
);
return
$model
->
delete
();
$delete
=
$model
->
delete
();
if
(
$delete
!==
false
)
{
return
$delete
;
}
else
{
throw
new
HttpResponseException
(
'删除失败,请刷新重试'
);
}
}
public
function
findBy
(
$field
,
$value
,
$columns
=
[
'*'
])
...
...
@@ -157,5 +194,4 @@ abstract class BaseRepository implements RepositoryInterface
return
$this
->
model
->
where
(
$field
,
'='
,
$value
)
->
first
(
$columns
);
}
}
\ No newline at end of file
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