Skip to content

Commit

Permalink
Support to use model builder as the column in `Hyperf\Database\Query\…
Browse files Browse the repository at this point in the history
…Builder\addSelect`. (#6259)
  • Loading branch information
limingxinleo committed Nov 3, 2023
1 parent 1d1da11 commit 80bb63e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\Exception\InvalidBindingException;
use Hyperf\Database\Model\Builder as ModelBuilder;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\Database\Query\Grammars\Grammar;
use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Macroable\Macroable;
Expand Down Expand Up @@ -366,9 +367,23 @@ public function fromRaw($expression, $bindings = [])
*/
public function addSelect($column)
{
$column = is_array($column) ? $column : func_get_args();
$columns = is_array($column) ? $column : func_get_args();

$this->columns = array_merge((array) $this->columns, $column);
foreach ($columns as $as => $column) {
if (is_string($as) && $this->isQueryable($column)) {
if (is_null($this->columns)) {
$this->select($this->from . '.*');
}

$this->selectSub($column, $as);
} else {
if (is_array($this->columns) && in_array($column, $this->columns, true)) {
continue;
}

$this->columns[] = $column;
}
}

return $this;
}
Expand Down Expand Up @@ -2663,6 +2678,20 @@ public function cloneWithoutBindings(array $except)
});
}

/**
* Determine if the value is a query builder instance or a Closure.
*
* @param mixed $value
* @return bool
*/
protected function isQueryable($value)
{
return $value instanceof static
|| $value instanceof ModelBuilder
|| $value instanceof Relation
|| $value instanceof Closure;
}

/**
* Creates a subquery and parse it.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ModelRealBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Hyperf\Support\Reflection\ClassInvoker;
use HyperfTest\Database\Stubs\ContainerStub;
use HyperfTest\Database\Stubs\IntegerStatus;
use HyperfTest\Database\Stubs\Model\Book;
use HyperfTest\Database\Stubs\Model\Gender;
use HyperfTest\Database\Stubs\Model\TestModel;
use HyperfTest\Database\Stubs\Model\TestVersionModel;
Expand Down Expand Up @@ -782,6 +783,23 @@ public function testCleanBindings()
$this->assertSame(['active', 1], $res);
}

public function testAddSelectObjects()
{
$this->getContainer();
$models = User::query()->addSelect([
'book_id' => Book::query()
->select('id')
->whereColumn('book.user_id', 'user.id')
->limit(1),
])->get();
$this->assertTrue($models->isNotEmpty());
while ($event = $this->channel->pop(0.001)) {
if ($event instanceof QueryExecuted) {
$this->assertSame($event->sql, 'select `user`.*, (select `id` from `book` where `book`.`user_id` = `user`.`id` limit 1) as `book_id` from `user`');
}
}
}

protected function getContainer()
{
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
Expand Down

0 comments on commit 80bb63e

Please sign in to comment.