Skip to content

Commit c1e15ce

Browse files
committed
fix: single taxonomy query
1 parent 8d1af5a commit c1e15ce

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Relations/WithTaxonomy.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,15 @@ protected function getLoadedItems()
9999
$this->termTableName . '.term_id', '=', $this->termTableName . '.term_id'
100100
)->whereIn($this->termRelationshipTable . '.object_id', $this->extractObjectKeys());
101101
})->join($this->termTaxonomyTable, function (Join $join) {
102-
$join->on($this->termTaxonomyTable . '.term_taxonomy_id', '=', $this->termRelationshipTable . '.term_taxonomy_id');
102+
$join->on($this->termTaxonomyTable . '.term_taxonomy_id', '=', $this->termRelationshipTable . '.term_taxonomy_id')
103+
->whereColumn($this->termTableName.'.term_id', '=', $this->termTaxonomyTable.'.term_id');
104+
103105
if (!empty($this->taxonomies)) {
104-
$join->whereIn($this->termTaxonomyTable . '.taxonomy', $this->taxonomies);
106+
if (count($this->taxonomies) === 1) {
107+
$join->where($this->termTaxonomyTable . '.taxonomy', $this->taxonomies[0]);
108+
} else {
109+
$join->whereIn($this->termTaxonomyTable . '.taxonomy', $this->taxonomies);
110+
}
105111
}
106112
})->get();
107113
}

tests/Unit/TaxonomyTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ function it_can_generate_correct_sql_query()
3737

3838
$b = new Builder($c);
3939

40-
$sql = 'select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id';
40+
$sql = 'select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_terms.term_id = wp_term_taxonomy.term_id';
4141

4242
(new WithTaxonomy('taxonomies', $b))->setItems([(object)['ID' => 1]])->load();
4343

4444
$this->assertEquals($sql, $b->toSQL());
4545

46-
$sql = "select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_term_taxonomy.taxonomy in (?)";
46+
$sql = "select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_terms.term_id = wp_term_taxonomy.term_id and wp_term_taxonomy.taxonomy = ?";
4747
$b = new Builder($c);
4848
(new WithTaxonomy('taxonomies', $b))
4949
->taxonomy('category')
@@ -52,6 +52,15 @@ function it_can_generate_correct_sql_query()
5252

5353
$this->assertEquals($sql, $b->toSQL());
5454

55+
$sql = "select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_terms.term_id = wp_term_taxonomy.term_id and wp_term_taxonomy.taxonomy in (?, ?)";
56+
$c = new Builder($c);
57+
(new WithTaxonomy('taxonomies', $c))
58+
->taxonomy(['category', 'tags'])
59+
->setItems([(object)['ID' => 1]])
60+
->load();
61+
62+
$this->assertEquals($sql, $c->toSQL());
63+
5564
m::close();
5665
}
5766

0 commit comments

Comments
 (0)