From 539d695b0fae526fbfe9b2e9d124a47e7acecd3e Mon Sep 17 00:00:00 2001 From: dimitri-bouteille Date: Sat, 9 Mar 2024 11:31:37 +0100 Subject: [PATCH] #34 Fix meta join, update WithMetaBuilderTest.php --- src/Builders/AbstractWithMetaBuilder.php | 3 +- tests/Builders/WithMetaBuilderTest.php | 68 +++++++++++------------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/Builders/AbstractWithMetaBuilder.php b/src/Builders/AbstractWithMetaBuilder.php index 3463071e..8601f980 100644 --- a/src/Builders/AbstractWithMetaBuilder.php +++ b/src/Builders/AbstractWithMetaBuilder.php @@ -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; /** @@ -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), '=', diff --git a/tests/Builders/WithMetaBuilderTest.php b/tests/Builders/WithMetaBuilderTest.php index ebdc3da0..5d1c57d8 100644 --- a/tests/Builders/WithMetaBuilderTest.php +++ b/tests/Builders/WithMetaBuilderTest.php @@ -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"', ]; } @@ -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"', ]; } @@ -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 ); } @@ -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, );