Skip to content

Commit

Permalink
Merge branch 'master' into 17-saveorfail-doesnt-throw-queryexception
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	tests/Builders/WithMetaBuilderTest.php
  • Loading branch information
dimitriBouteille committed Mar 9, 2024
2 parents acc27cd + ba3296f commit 2f30c17
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/styles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

steps:
- name: Checkout repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Configure PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

steps:
- name: Checkout repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Configure PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"phpunit/phpunit": "^10.5",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/extension-installer": "^1.3",
"roots/wordpress": "6.4",
"roots/wordpress": "6.4.3",
"rector/rector": "1.0.2",
"szepeviktor/phpstan-wordpress": "^1.3"
},
Expand Down
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
14 changes: 7 additions & 7 deletions tests/Builders/WithMetaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public static function providerTestAddMetaToSelect(): \Generator
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"',
'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"',
'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 Down Expand Up @@ -108,21 +108,21 @@ public static function providerTestAddMetasToSelect(): \Generator
'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"',
'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',
],
'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 "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"',
'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 @@ -137,7 +137,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 @@ -153,7 +153,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 2f30c17

Please sign in to comment.