Skip to content

Commit

Permalink
Add an attribute to force query object get columns from DB #1122
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 10, 2024
1 parent aa150fd commit fc788e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/orm/src/Attributes/UseRealColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

/**
* Use real DB columns when Query auto-join and prepare columns.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class UseRealColumns
{
}
16 changes: 11 additions & 5 deletions packages/orm/src/SelectorQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Windwalker\ORM;

use InvalidArgumentException;
use Windwalker\Attributes\AttributesAccessor;
use Windwalker\Data\Collection;
use Windwalker\Database\DatabaseAdapter;
use Windwalker\Database\Event\HydrateEvent;
use Windwalker\Database\Event\ItemFetchedEvent;
use Windwalker\Event\EventAwareInterface;
use Windwalker\Event\EventAwareTrait;
use Windwalker\ORM\Attributes\UseRealColumns;
use Windwalker\ORM\Attributes\Mapping;
use Windwalker\ORM\Metadata\EntityMetadata;
use Windwalker\ORM\Relation\Strategy\ManyToMany;
Expand Down Expand Up @@ -112,12 +114,16 @@ public function autoSelections(string $divider = '.', ?array &$columns = null):

$tableName = static::convertClassToTable($className, $alias);

if (class_exists($className)) {
$cols = array_keys($this->orm->getEntityMetadata($className)->getPureColumns());
} else {
$tbm = $db->getTable($tableName);
$loadColsFromDb = !class_exists($className)
|| (
class_exists($className)
&& AttributesAccessor::getFirstAttribute($className, UseRealColumns::class)
);

$cols = $tbm->getColumnNames();
if ($loadColsFromDb) {
$cols = $db->getTable($tableName)->getColumnNames();
} else {
$cols = array_keys($this->orm->getEntityMetadata($className)->getPureColumns());
}

foreach ($cols as $col) {
Expand Down

0 comments on commit fc788e8

Please sign in to comment.