Prefix attribute with table name in SoftDeleteQueryBehavior methods (deleted and unDeleted); Fix #2

parent c4d8850f
...@@ -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');
}
} }
...@@ -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);
}
} }
...@@ -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']);
}
} }
...@@ -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']);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment