Skip to content

Commit

Permalink
Merge pull request #447: fix wrong adding table prefix on join
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Dec 8, 2023
2 parents 655975e + a6da9b1 commit 5d19075
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Select/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public function __construct(
*/
public function __call(string $func, array $args)
{
$result = \call_user_func_array($this->targetFunc($func), $this->proxyArgs($args));
$result = \call_user_func_array(
$this->targetFunc($func),
$this->isJoin($func) ? $args : $this->proxyArgs($args)
);

if ($result === $this->query) {
return $this;
}
Expand Down Expand Up @@ -277,4 +281,9 @@ private function walkRecursive(array $input, callable $func, bool $complex = fal

return $result;
}

private function isJoin(string $method): bool
{
return \in_array($method, ['join', 'innerJoin', 'rightJoin', 'leftJoin', 'fullJoin'], true);
}
}
108 changes: 108 additions & 0 deletions tests/ORM/Functional/Driver/Common/Integration/Case398/CaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398;

use Cycle\Database\Injection\Fragment;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\Entity\Product;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
use Cycle\ORM\Tests\Traits\TableTrait;

abstract class CaseTest extends BaseTest
{
use IntegrationTestTrait;
use TableTrait;

public function setUp(): void
{
parent::setUp();
$this->makeTables();
$this->fillData();

$this->loadSchema(__DIR__ . '/schema.php');
}

public function testSelectWithJoin(): void
{
$select = new Select($this->orm, Product::class);
$product = $select
->join('inner', 'filter_products', 'fp')->on('id', 'fp.product_id')
->where(new Fragment('fp.filter_id = ?', 5))
->fetchOne();

$this->assertSame('Product-2', $product->title);
}

public function testSelectWithInnerJoin(): void
{
$select = new Select($this->orm, Product::class);
$product = $select
->innerJoin('filter_products', 'fp')->on('id', 'fp.product_id')
->where(new Fragment('fp.filter_id = ?', 5))
->fetchOne();

$this->assertSame('Product-2', $product->title);
}

public function testSelectWithRightJoin(): void
{
$select = new Select($this->orm, Product::class);
$product = $select
->rightJoin('filter_products', 'fp')->on('id', 'fp.product_id')
->where(new Fragment('fp.filter_id = ?', 5))
->fetchOne();

$this->assertSame('Product-2', $product->title);
}

public function testSelectWithLeftJoin(): void
{
$select = new Select($this->orm, Product::class);
$product = $select
->leftJoin('filter_products', 'fp')->on('id', 'fp.product_id')
->where(new Fragment('fp.filter_id = ?', 5))
->fetchOne();

$this->assertSame('Product-2', $product->title);
}

public function testSelectWithFullJoin(): void
{
$select = new Select($this->orm, Product::class);
$product = $select
->fullJoin('filter_products', 'fp')->on('id', 'fp.product_id')
->where(new Fragment('fp.filter_id = ?', 5))
->fetchOne();

$this->assertSame('Product-2', $product->title);
}

private function makeTables(): void
{
$this->makeTable('products', [
'id' => 'primary',
'title' => 'string',
]);

$this->makeTable('filter_products', [
'filter_id' => 'int',
'product_id' => 'int',
], pk: ['filter_id', 'product_id']);
}

private function fillData(): void
{
$this->getDatabase()->table('products')->insertMultiple(
['title'],
[['Product-1'], ['Product-2'], ['Product-3']],
);

$this->getDatabase()->table('filter_products')->insertMultiple(
['filter_id', 'product_id'],
[[1, 1], [5, 2], [6, 3]],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\Entity;

class FilterProduct
{
public const ROLE = 'filter_product';

public function __construct(
public int $productId,
public int $filterId,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\Entity;

class Product
{
public const ROLE = 'product';

public int $id;

public function __construct(
public string $title,
) {
}
}
48 changes: 48 additions & 0 deletions tests/ORM/Functional/Driver/Common/Integration/Case398/schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\SchemaInterface as Schema;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\Entity\FilterProduct;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\Entity\Product;

return [
Product::ROLE => [
Schema::ENTITY => Product::class,
Schema::SOURCE => Source::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'products',
Schema::PRIMARY_KEY => ['id'],
Schema::COLUMNS => [
'id' => 'id',
'title' => 'title',
],
Schema::RELATIONS => [],
Schema::TYPECAST => [
'id' => 'int',
],
Schema::SCHEMA => [],
],
FilterProduct::ROLE => [
Schema::ENTITY => FilterProduct::class,
Schema::SOURCE => Source::class,
Schema::DATABASE => 'default',
Schema::MAPPER => Mapper::class,
Schema::TABLE => 'filter_products_table',
Schema::PRIMARY_KEY => ['product_id', 'filter_id'],
Schema::COLUMNS => [
'id' => 'id',
'filterId' => 'filter_id',
'productId' => 'product_id',
],
Schema::RELATIONS => [
],
Schema::TYPECAST => [
'productId' => 'int',
'filterId' => 'int',
],
Schema::SCHEMA => [],
],
];
22 changes: 22 additions & 0 deletions tests/ORM/Functional/Driver/MySQL/Integration/Case398/CaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\MySQL\Integration\Case398;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\CaseTest as CommonClass;

/**
* @group driver
* @group driver-mysql
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'mysql';

public function testSelectWithFullJoin(): void
{
$this->markTestSkipped('MySQL does not support FULL JOIN');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Postgres\Integration\Case398;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\CaseTest as CommonClass;

/**
* @group driver
* @group driver-postgres
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'postgres';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLServer\Integration\Case398;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlserver
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlserver';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLite\Integration\Case398;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case398\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlite
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlite';

public function testSelectWithFullJoin(): void
{
$this->markTestSkipped('SQLite does not support FULL JOIN');
}

public function testSelectWithRightJoin(): void
{
$this->markTestSkipped('SQLite does not support RIGHT JOIN');
}
}

0 comments on commit 5d19075

Please sign in to comment.