Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove $tableName property from constructor #366

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ abstract class AbstractActiveRecord implements ActiveRecordInterface
private array $relationsDependencies = [];

public function __construct(
private ConnectionInterface $db,
private string $tableName = ''
private ConnectionInterface $db
) {
}

Expand Down Expand Up @@ -1250,11 +1249,7 @@ protected function resetDependentRelations(string $attribute): void

public function getTableName(): string
{
if ($this->tableName === '') {
$this->tableName = '{{%' . DbStringHelper::pascalCaseToId(DbStringHelper::baseName(static::class)) . '}}';
}

return $this->tableName;
return '{{%' . DbStringHelper::pascalCaseToId(DbStringHelper::baseName(static::class)) . '}}';
}

protected function db(): ConnectionInterface
Expand Down
5 changes: 2 additions & 3 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
final public function __construct(
protected string|ActiveRecordInterface|Closure $arClass,
protected ConnectionInterface $db,
private string $tableName = ''
protected ConnectionInterface $db
) {
parent::__construct($db);
}
Expand Down Expand Up @@ -1002,7 +1001,7 @@ public function getARInstance(): ActiveRecordInterface
/** @psalm-var class-string<ActiveRecordInterface> $class */
$class = $this->arClass;

return new $class($this->db, $this->tableName);
return new $class($this->db);
}

private function createInstance(): static
Expand Down
25 changes: 9 additions & 16 deletions src/ActiveRecordFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace Yiisoft\ActiveRecord;

use Closure;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Definitions\Exception\CircularReferenceException;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Exception\NotInstantiableException;
use Yiisoft\Factory\Factory;
use Yiisoft\Factory\NotFoundException;

/**
* @psalm-import-type ARClass from ActiveQueryInterface
*/
final class ActiveRecordFactory
{
public function __construct(private Factory $factory)
Expand All @@ -21,8 +25,6 @@ public function __construct(private Factory $factory)
* Allows you to create an active record instance through the factory.
*
* @param string $arClass active record class.
* @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the
* name will be generated automatically by calling {@see getTableName()} in the active record class.
* @param ConnectionInterface|null $db the database connection used for creating active record instances.
*
* @throws CircularReferenceException
Expand All @@ -37,16 +39,11 @@ public function __construct(private Factory $factory)
*/
public function createAR(
string $arClass,
string $tableName = '',
ConnectionInterface $db = null
): ActiveRecordInterface {
$params = [];
$params['class'] = $arClass;

if ($tableName !== '') {
$params['__construct()']['tableName'] = $tableName;
}

if ($db !== null) {
$params['__construct()']['db'] = $db;
}
Expand All @@ -57,20 +54,20 @@ public function createAR(
/**
* Allows you to create an active query instance through the factory.
*
* @param string $arClass active record class.
* @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the
* name will be generated automatically by calling {@see getTableName()} in the active record class.
* @param ActiveRecordInterface|Closure|string $arClass the active record class, active record instance or closure
* returning active record instance.
* @param string $queryClass custom query active query class.
* @param ConnectionInterface|null $db the database connection used for creating active query instances.
*
* @throws CircularReferenceException
* @throws InvalidConfigException
* @throws NotFoundException
* @throws NotInstantiableException
*
* @psalm-param ARClass $arClass
*/
public function createQueryTo(
string $arClass,
string $tableName = '',
string|ActiveRecordInterface|Closure $arClass,
string $queryClass = ActiveQuery::class,
ConnectionInterface $db = null
): ActiveQueryInterface {
Expand All @@ -81,10 +78,6 @@ public function createQueryTo(
],
];

if ($tableName !== '') {
$params['__construct()']['tableName'] = $tableName;
}

if ($db !== null) {
$params['__construct()']['db'] = $db;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Trait/CustomTableNameTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Trait;

/**
* Trait to implement custom table name for ActiveRecord.
*
* @see ActiveRecordInterface::getTableName()
*/
trait CustomTableNameTrait
{
private string $tableName;

/**
* Sets the table name for the ActiveRecord.
*/
public function withTableName(string $tableName): static
{
$new = clone $this;
$new->tableName = $tableName;
return $new;
}

public function getTableName(): string
{
return $this->tableName ??= parent::getTableName();
}
}
13 changes: 11 additions & 2 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1851,8 +1851,17 @@ public function testRelationWhereParams(string $orderTableName, string $orderIte

$this->checkFixture($this->db, 'order');

$order = new Order(db: $this->db, tableName: $orderTableName);
$orderItem = new OrderItem(db: $this->db, tableName: $orderItemTableName);
$order = new Order($this->db);
$orderItem = new OrderItem($this->db);

$this->assertSame('order', $order->getTableName());
$this->assertSame('order_item', $orderItem->getTableName());

$order = $order->withTableName($orderTableName);
$orderItem = $orderItem->withTableName($orderItemTableName);

$this->assertSame($orderTableName, $order->getTableName());
$this->assertSame($orderItemTableName, $orderItem->getTableName());

$orderQuery = new ActiveQuery(Order::class, $this->db);
$order = $orderQuery->findOne(1);
Expand Down
24 changes: 15 additions & 9 deletions tests/ActiveRecordFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\ActiveRecordFactory;
use Yiisoft\ActiveRecord\ActiveRecordInterface;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\ArrayAndJsonTypes;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerQuery;
Expand Down Expand Up @@ -33,11 +34,15 @@ public function testCreateARWithConnection(): void

public function testCreateARWithTableName(): void
{
$customerAR = $this->arFactory->createAR(ArrayAndJsonTypes::class, 'array_and_json_types');
$tableName = $customerAR->getTableName();
$model = $this->arFactory->createAR(ArrayAndJsonTypes::class);

$this->assertSame('array_and_json_types', $tableName);
$this->assertInstanceOf(ArrayAndJsonTypes::class, $customerAR);
$this->assertSame('{{%array_and_json_types}}', $model->getTableName());
$this->assertInstanceOf(ArrayAndJsonTypes::class, $model);

$model = $model->withTableName('array_and_json_types');

$this->assertSame('array_and_json_types', $model->getTableName());
$this->assertInstanceOf(ArrayAndJsonTypes::class, $model);
}

public function testCreateQueryTo(): void
Expand Down Expand Up @@ -70,14 +75,15 @@ public function testCreateQueryToWithConnection(): void
public function testCreateQueryToWithTableName(): void
{
/** example create active query */
$customerQuery = $this->arFactory->createQueryTo(
arClass: ArrayAndJsonTypes::class,
tableName: 'array_and_json_types',
$modelQuery = $this->arFactory->createQueryTo(
arClass: fn (): ActiveRecordInterface => $this->arFactory
->createAR(ArrayAndJsonTypes::class)
->withTableName('array_and_json_types'),
);
$tableName = $customerQuery->getARInstance()->getTableName();
$tableName = $modelQuery->getARInstance()->getTableName();

$this->assertSame('array_and_json_types', $tableName);
$this->assertInstanceOf(ActiveQuery::class, $customerQuery);
$this->assertInstanceOf(ActiveQuery::class, $modelQuery);
}

public function testGetArInstanceWithConstructor(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;

use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
use Yiisoft\ActiveRecord\Trait\CustomTableNameTrait;
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\JsonExpression;

final class ArrayAndJsonTypes extends ActiveRecord
{
use CustomTableNameTrait;

public int $id;
public array|ArrayExpression|null $intarray_col = null;
public array|ArrayExpression|null $textarray2_col = null;
Expand Down
4 changes: 2 additions & 2 deletions tests/Stubs/ActiveRecord/CustomerWithConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class CustomerWithConstructor extends ActiveRecord
protected bool|string|null $bool_status = false;
protected int|null $profile_id = null;

public function __construct(ConnectionInterface $db, string $tableName = '', private Aliases|null $aliases = null)
public function __construct(ConnectionInterface $db, private Aliases|null $aliases = null)
{
parent::__construct($db, $tableName);
parent::__construct($db);
}

public function getTableName(): string
Expand Down
5 changes: 4 additions & 1 deletion tests/Stubs/ActiveRecord/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\ActiveQueryInterface;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
use Yiisoft\ActiveRecord\Trait\CustomTableNameTrait;

/**
* Class Order.
*/
class Order extends ActiveRecord
{
use CustomTableNameTrait;

public const TABLE_NAME = 'order';

protected int|null $id;
Expand All @@ -24,7 +27,7 @@ class Order extends ActiveRecord

public function getTableName(): string
{
return self::TABLE_NAME;
return $this->tableName ??= self::TABLE_NAME;
}

public function getId(): int|null
Expand Down
5 changes: 4 additions & 1 deletion tests/Stubs/ActiveRecord/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\ActiveQueryInterface;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
use Yiisoft\ActiveRecord\Trait\CustomTableNameTrait;

/**
* Class OrderItem.
*/
final class OrderItem extends ActiveRecord
{
use CustomTableNameTrait;

protected int $order_id;
protected int $item_id;
protected int $quantity;
protected float $subtotal;

public function getTableName(): string
{
return 'order_item';
return $this->tableName ??= 'order_item';
}

public function fields(): array
Expand Down
Loading