Commit f09c0a38 authored by Brett O'Donnell's avatar Brett O'Donnell Committed by GitHub

Merge pull request #3 from elvenpath/fix-ambiguous-error

Prefix attribute with table name in SoftDeleteQueryBehavior methods (…
parents c4d8850f 6e65068e
......@@ -10,6 +10,7 @@ namespace cornernote\softdelete;
use yii\base\Behavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* SoftDeleteQueryBehavior
......@@ -36,19 +37,26 @@ class SoftDeleteQueryBehavior extends Behavior
public $attribute = 'deleted_at';
/**
* @return static
* @return ActiveQuery
*/
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()
{
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
$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 @@
namespace tests\models;
use common\models\ProductQuery;
use cornernote\softdelete\SoftDeleteBehavior;
use yii\db\ActiveRecord;
......@@ -47,4 +48,12 @@ class PostA extends ActiveRecord
{
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
];
}
/**
* @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