From 0d713eebb75c315cbfcbb901025ac6241b5223b3 Mon Sep 17 00:00:00 2001 From: Josh Pollock <josh@calderawp.com> Date: Thu, 5 Apr 2018 21:02:54 -0400 Subject: [PATCH] delete entry values by entry id --- Tests/Integration/Delete/EntryValuesTest.php | 36 ++++++++++++++++++++ src/Delete/EntryValues.php | 12 +++++++ src/QueryBuilder.php | 9 ++--- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Tests/Integration/Delete/EntryValuesTest.php b/Tests/Integration/Delete/EntryValuesTest.php index eec8152..817a022 100644 --- a/Tests/Integration/Delete/EntryValuesTest.php +++ b/Tests/Integration/Delete/EntryValuesTest.php @@ -4,6 +4,7 @@ namespace calderawp\CalderaFormsQuery\Tests\Integration\Delete; +use calderawp\CalderaFormsQuery\Delete\Entry; use calderawp\CalderaFormsQuery\Delete\EntryValues; use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase; use calderawp\CalderaFormsQuery\Tests\Traits\CanCreateEntryWithEmailField; @@ -79,4 +80,39 @@ class EntryValuesTest extends IntegrationTestCase } + /** + * + * + * @covers EntryValues::deleteByEntryIds() + */ + public function testDeleteByEntryIds() + { + $entryIdOne = $this->createEntryWithMockFormAndGetEntryId(); + $entryIdTwo = $this->createEntryWithMockFormAndGetEntryId(); + $entryIdThree = $this->createEntryWithMockFormAndGetEntryId(); + //Delete entry one and three + $sql = $this + ->entryValuesDeleteGeneratorFactory() + ->deleteByEntryIds([$entryIdOne,$entryIdThree]) + ->getPreparedSql(); + + $this->queryWithWPDB($sql); + + + //No results for entry One + $entryValuesQueryGenerator = $this->entryValuesGeneratorFactory(); + $entryValuesQueryGenerator->queryByEntryId($entryIdOne); + $sql = $entryValuesQueryGenerator->getPreparedSql(); + $results = $this->queryWithWPDB($sql); + $this->assertSame( 0, count( $results ) ); + + //Results for entry Two + $entryValuesQueryGenerator = $this->entryValuesGeneratorFactory(); + $entryValuesQueryGenerator->queryByEntryId($entryIdTwo); + $sql = $entryValuesQueryGenerator->getPreparedSql(); + $results = $this->queryWithWPDB($sql); + $this->assertTrue( 0 < count( $results ) ); + + } + } \ No newline at end of file diff --git a/src/Delete/EntryValues.php b/src/Delete/EntryValues.php index 4995cb7..f189a6b 100644 --- a/src/Delete/EntryValues.php +++ b/src/Delete/EntryValues.php @@ -22,6 +22,18 @@ class EntryValues extends DeleteQueryBuilder return $this; } + /** + * Delete a collection of entry values that are for a a set of entries. + * + * @param array $entryIds + * @return $this + */ + public function deleteByEntryIds(array $entryIds) + { + return $this->in($entryIds,'entry_id'); + + } + /** * Delete all field values with a value * diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 6dc87a8..6f47af1 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -160,17 +160,18 @@ abstract class QueryBuilder implements CreatesSqlQueries } /** - * Add a WHERE IN() + * Add a WHERE IN() $column * - * @param array $entryIds + * @param array $entryIds Entries to search in + * @param string $column Column name. Default is 'id'. * @return $this */ - protected function in(array $entryIds) + protected function in(array $entryIds, $column = 'id') { $this ->getCurrentQuery() ->where() - ->in('id', $entryIds); + ->in($column, $entryIds); return $this; } } -- GitLab