Skip to content

Commit

Permalink
#34 Fix meta join, update WithMetaBuilderTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Mar 9, 2024
1 parent abeb19e commit 539d695
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/Builders/AbstractWithMetaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Dbout\WpOrm\Exceptions\WpOrmException;
use Dbout\WpOrm\MetaMappingConfig;
use Dbout\WpOrm\Orm\AbstractModel;
use Dbout\WpOrm\Orm\Database;
use Illuminate\Database\Eloquent\Model;

/**
Expand Down Expand Up @@ -128,7 +129,7 @@ public function joinToMeta(string $metaKey, string $joinType = 'inner'): self
$join->on(
sprintf('%s.%s', $metaKey, $this->metaConfig?->columnKey),
'=',
"$metaKey"
Database::getInstance()->raw(sprintf("'%s'", $metaKey))
)->on(
sprintf('%s.%s', $metaKey, $this->metaConfig?->foreignKey),
'=',
Expand Down
68 changes: 32 additions & 36 deletions tests/Builders/WithMetaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,19 @@ public function testAddMetaToSelect(string $metaKey, ?string $alias, string $exp
}

/**
* @return array
* @return \Generator
*/
public static function providerTestAddMetaToSelect(): array
public static function providerTestAddMetaToSelect(): \Generator
{
return [
'Without alias' => [
'my_meta',
null,
'select "posts".*, "my_meta"."meta_value" as "my_meta_value" from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = "my_meta" and "my_meta"."post_id" = "posts"."ID"',
],
'With alias' => [
'first_name',
'my_custom_alias',
'select "posts".*, "first_name"."meta_value" as "my_custom_alias" from "posts" inner join "postmeta" as "first_name" on "first_name"."meta_key" = "first_name" and "first_name"."post_id" = "posts"."ID"',
],
yield 'Without alias' => [
'my_meta',
null,
'select "posts".*, "my_meta"."meta_value" as "my_meta_value" from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = \'my_meta\' and "my_meta"."post_id" = "posts"."ID"',
];
yield 'With alias' => [
'first_name',
'my_custom_alias',
'select "posts".*, "first_name"."meta_value" as "my_custom_alias" from "posts" inner join "postmeta" as "first_name" on "first_name"."meta_key" = \'first_name\' and "first_name"."post_id" = "posts"."ID"',
];
}

Expand All @@ -96,32 +94,30 @@ public function testAddMetasToSelect(array $metas, string $expectedQuery): void
}

/**
* @return array
* @return \Generator
*/
public static function providerTestAddMetasToSelect(): array
public static function providerTestAddMetasToSelect(): \Generator
{
return [
'Without alias' => [
[
'firstname',
'lastname',
],
'select "posts".*, "firstname"."meta_value" as "firstname_value", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
yield 'Without alias' => [
[
'firstname',
'lastname',
],
'With alias' => [
[
'my_meta' => 'firstname',
'second_meta' => 'lastname',
],
'select "posts".*, "firstname"."meta_value" as "my_meta", "lastname"."meta_value" as "second_meta" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
'select "posts".*, "firstname"."meta_value" as "firstname_value", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = \'firstname\' and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = \'lastname\' and "lastname"."post_id" = "posts"."ID"',
];
yield 'With alias' => [
[
'my_meta' => 'firstname',
'second_meta' => 'lastname',
],
'On meta with alias on another one without alias' => [
[
'my_meta' => 'street_1',
'lastname',
],
'select "posts".*, "street_1"."meta_value" as "my_meta", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "street_1" on "street_1"."meta_key" = "street_1" and "street_1"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
'select "posts".*, "firstname"."meta_value" as "my_meta", "lastname"."meta_value" as "second_meta" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = \'firstname\' and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = \'lastname\' and "lastname"."post_id" = "posts"."ID"',
];
yield 'On meta with alias on another one without alias' => [
[
'my_meta' => 'street_1',
'lastname',
],
'select "posts".*, "street_1"."meta_value" as "my_meta", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "street_1" on "street_1"."meta_key" = \'street_1\' and "street_1"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = \'lastname\' and "lastname"."post_id" = "posts"."ID"',
];
}

Expand All @@ -136,7 +132,7 @@ public function testJoinToMeta(): void
$this->builder->joinToMeta('my_meta');
$query = $this->builder->toSql();
$this->assertEquals(
'select "posts".* from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = "my_meta" and "my_meta"."post_id" = "posts"."ID"',
'select "posts".* from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = \'my_meta\' and "my_meta"."post_id" = "posts"."ID"',
$query
);
}
Expand All @@ -152,7 +148,7 @@ public function testAddMetaToFilter(): void
$query = $this->builder->toSql();

$this->assertEquals(
'select "posts".* from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" where "firstname"."meta_value" = ?',
'select "posts".* from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = \'firstname\' and "firstname"."post_id" = "posts"."ID" where "firstname"."meta_value" = ?',
$query,
);

Expand Down

0 comments on commit 539d695

Please sign in to comment.