Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
Y
yii2-softdelete
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
digisin
yii2-softdelete
Commits
6e65068e
Commit
6e65068e
authored
Sep 18, 2016
by
vlad.nedelcu@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prefix attribute with table name in SoftDeleteQueryBehavior methods (deleted and unDeleted); Fix #2
parent
c4d8850f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
5 deletions
+42
-5
SoftDeleteQueryBehavior.php
src/SoftDeleteQueryBehavior.php
+13
-5
SoftDeleteQueryTest.php
tests/unit/SoftDeleteQueryTest.php
+13
-0
PostA.php
tests/unit/models/PostA.php
+9
-0
PostB.php
tests/unit/models/PostB.php
+7
-0
No files found.
src/SoftDeleteQueryBehavior.php
View file @
6e65068e
...
@@ -10,6 +10,7 @@ namespace cornernote\softdelete;
...
@@ -10,6 +10,7 @@ namespace cornernote\softdelete;
use
yii\base\Behavior
;
use
yii\base\Behavior
;
use
yii\db\ActiveQuery
;
use
yii\db\ActiveQuery
;
use
yii\db\ActiveRecord
;
/**
/**
* SoftDeleteQueryBehavior
* SoftDeleteQueryBehavior
...
@@ -36,19 +37,26 @@ class SoftDeleteQueryBehavior extends Behavior
...
@@ -36,19 +37,26 @@ class SoftDeleteQueryBehavior extends Behavior
public
$attribute
=
'deleted_at'
;
public
$attribute
=
'deleted_at'
;
/**
/**
* @return
static
* @return
ActiveQuery
*/
*/
public
function
deleted
()
public
function
deleted
()
{
{
return
$this
->
owner
->
andWhere
(
$this
->
attribute
.
' IS NOT NULL'
);
/** @var ActiveRecord $modelClass */
$modelClass
=
$this
->
owner
->
modelClass
;
$tableName
=
$modelClass
::
tableName
();
return
$this
->
owner
->
andWhere
(
$tableName
.
'.'
.
$this
->
attribute
.
' IS NOT NULL'
);
}
}
/**
/**
* @return
static
* @return
ActiveQuery
*/
*/
public
function
notDeleted
()
public
function
notDeleted
()
{
{
return
$this
->
owner
->
andWhere
(
$this
->
attribute
.
' IS NULL'
);
/** @var ActiveRecord $modelClass */
}
$modelClass
=
$this
->
owner
->
modelClass
;
$tableName
=
$modelClass
::
tableName
();
return
$this
->
owner
->
andWhere
(
$tableName
.
'.'
.
$this
->
attribute
.
' IS NULL'
);
}
}
}
tests/unit/SoftDeleteQueryTest.php
View file @
6e65068e
...
@@ -43,4 +43,17 @@ class SoftDeleteQueryTest extends DatabaseTestCase
...
@@ -43,4 +43,17 @@ class SoftDeleteQueryTest extends DatabaseTestCase
$this
->
assertEquals
(
require
(
__DIR__
.
'/data/test-find-not-deleted-posts.php'
),
$data
);
$this
->
assertEquals
(
require
(
__DIR__
.
'/data/test-find-not-deleted-posts.php'
),
$data
);
}
}
/**
* Find Not Deleted Posts
*/
public
function
testFindNotDeletedPostsWithJoin
()
{
$data
=
[];
$posts
=
PostA
::
find
()
->
notDeleted
()
->
joinWith
(
'postB'
)
->
all
();
foreach
(
$posts
as
$post
)
{
$data
[]
=
$post
->
toArray
();
}
$this
->
assertEquals
(
require
(
__DIR__
.
'/data/test-find-not-deleted-posts.php'
),
$data
);
}
}
}
tests/unit/models/PostA.php
View file @
6e65068e
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
namespace
tests\models
;
namespace
tests\models
;
use
common\models\ProductQuery
;
use
cornernote\softdelete\SoftDeleteBehavior
;
use
cornernote\softdelete\SoftDeleteBehavior
;
use
yii\db\ActiveRecord
;
use
yii\db\ActiveRecord
;
...
@@ -47,4 +48,12 @@ class PostA extends ActiveRecord
...
@@ -47,4 +48,12 @@ class PostA extends ActiveRecord
{
{
return
new
PostQuery
(
get_called_class
());
return
new
PostQuery
(
get_called_class
());
}
}
/**
* @return \yii\db\ActiveQuery|ProductQuery
*/
public
function
getPostB
()
{
return
$this
->
hasOne
(
PostB
::
className
(),
[
'id'
=>
'id'
]);
}
}
}
tests/unit/models/PostB.php
View file @
6e65068e
...
@@ -45,4 +45,11 @@ class PostB extends ActiveRecord
...
@@ -45,4 +45,11 @@ class PostB extends ActiveRecord
];
];
}
}
/**
* @return \yii\db\ActiveQuery|ProductQuery
*/
public
function
getPostA
()
{
return
$this
->
hasOne
(
PostA
::
className
(),
[
'id'
=>
'id'
]);
}
}
}
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