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

[8.x] QueryBuilder add clone() #34780

Merged
merged 2 commits into from Oct 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Expand Up @@ -3233,6 +3233,16 @@ protected function isQueryable($value)
$value instanceof Closure;
}

/**
* Clone the query.
*
* @return static
*/
public function clone()
{
return clone $this;
}

/**
* Clone the query without the given properties.
*
Expand All @@ -3241,7 +3251,7 @@ protected function isQueryable($value)
*/
public function cloneWithout(array $properties)
{
return tap(clone $this, function ($clone) use ($properties) {
return tap($this->clone(), function ($clone) use ($properties) {
foreach ($properties as $property) {
$clone->{$property} = null;
}
Expand All @@ -3256,7 +3266,7 @@ public function cloneWithout(array $properties)
*/
public function cloneWithoutBindings(array $except)
{
return tap(clone $this, function ($clone) use ($except) {
return tap($this->clone(), function ($clone) use ($except) {
foreach ($except as $type) {
$clone->bindings[$type] = [];
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -3749,6 +3749,40 @@ public function testFromQuestionMarkOperatorOnPostgres()
$this->assertSame('select * from "users" where "roles" ??& ?', $builder->toSql());
}

public function testClone()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users');
$clone = $builder->clone()->where('email', 'foo');

$this->assertNotSame($builder, $clone);
$this->assertSame('select * from "users"', $builder->toSql());
$this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
}

public function testCloneWithout()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
$clone = $builder->cloneWithout(['orders']);

$this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
$this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
}

public function testCloneWithoutBindings()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
$clone = $builder->cloneWithout(['wheres'])->cloneWithoutBindings(['where']);

$this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
$this->assertEquals([0 => 'foo'], $builder->getBindings());

$this->assertSame('select * from "users" order by "email" asc', $clone->toSql());
$this->assertEquals([], $clone->getBindings());
}

protected function getConnection()
{
$connection = m::mock(ConnectionInterface::class);
Expand Down