Skip to content

Commit 9161736

Browse files
committed
Merge pull request #6638 from jlyu/select-from-write-pdo
[4.2] Select From Write Pdo
2 parents f706767 + 018a9f4 commit 9161736

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,18 @@ public static function find($id, $columns = array('*'))
644644
return $instance->newQuery()->find($id, $columns);
645645
}
646646

647+
/**
648+
* Begin querying the model on the write connection.
649+
*
650+
* @return \Illuminate\Database\Query\Builder
651+
*/
652+
public static function onWrite()
653+
{
654+
$instance = new static;
655+
656+
return $instance->newQuery()->useWritePdo();
657+
}
658+
647659
/**
648660
* Find a model by its primary key or return new static.
649661
*

src/Illuminate/Database/Query/Builder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ class Builder {
180180
'rlike', 'regexp', 'not regexp',
181181
);
182182

183+
/**
184+
* Whether use write pdo for select.
185+
*
186+
* @var bool
187+
*/
188+
protected $useWritePdo = false;
189+
183190
/**
184191
* Create a new query builder instance.
185192
*
@@ -1356,6 +1363,11 @@ public function getFresh($columns = array('*'))
13561363
*/
13571364
protected function runSelect()
13581365
{
1366+
if ($this->useWritePdo)
1367+
{
1368+
return $this->connection->select($this->toSql(), $this->getBindings(), false);
1369+
}
1370+
13591371
return $this->connection->select($this->toSql(), $this->getBindings());
13601372
}
13611373

@@ -2092,6 +2104,18 @@ public function getGrammar()
20922104
return $this->grammar;
20932105
}
20942106

2107+
/**
2108+
* Use the write pdo for query.
2109+
*
2110+
* @return $this
2111+
*/
2112+
public function useWritePdo()
2113+
{
2114+
$this->useWritePdo = true;
2115+
2116+
return $this;
2117+
}
2118+
20952119
/**
20962120
* Handle dynamic method calls into the method.
20972121
*

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function testFindMethodCallsQueryBuilderCorrectly()
105105
$this->assertEquals('foo', $result);
106106
}
107107

108+
public function testFindMethodUseWritePdo()
109+
{
110+
$result = EloquentModelFindWithWritePdoStub::onWrite()->find(1);
111+
}
112+
108113
/**
109114
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
110115
*/
@@ -1049,6 +1054,17 @@ public function newQuery()
10491054
}
10501055
}
10511056

1057+
class EloquentModelFindWithWritePdoStub extends Illuminate\Database\Eloquent\Model {
1058+
public function newQuery()
1059+
{
1060+
$mock = m::mock('Illuminate\Database\Eloquent\Builder');
1061+
$mock->shouldReceive('useWritePdo')->once()->andReturnSelf();
1062+
$mock->shouldReceive('find')->once()->with(1)->andReturn('foo');
1063+
1064+
return $mock;
1065+
}
1066+
}
1067+
10521068
class EloquentModelFindNotFoundStub extends Illuminate\Database\Eloquent\Model {
10531069
public function newQuery()
10541070
{

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ public function testBasicSelect()
2020
}
2121

2222

23+
public function testBasicSelectUseWritePdo()
24+
{
25+
$builder = $this->getMySqlBuilderWithProcessor();
26+
$builder->getConnection()->shouldReceive('select')->once()
27+
->with('select * from `users`', array(), false);
28+
$builder->useWritePdo()->select('*')->from('users')->get();
29+
30+
$builder = $this->getMySqlBuilderWithProcessor();
31+
$builder->getConnection()->shouldReceive('select')->once()
32+
->with('select * from `users`', array());
33+
$builder->select('*')->from('users')->get();
34+
}
35+
36+
2337
public function testBasicTableWrappingProtectsQuotationMarks()
2438
{
2539
$builder = $this->getBuilder();
@@ -1253,4 +1267,12 @@ protected function getSqlServerBuilder()
12531267
return new Builder(m::mock('Illuminate\Database\ConnectionInterface'), $grammar, $processor);
12541268
}
12551269

1270+
1271+
protected function getMySqlBuilderWithProcessor()
1272+
{
1273+
$grammar = new Illuminate\Database\Query\Grammars\MySqlGrammar;
1274+
$processor = new Illuminate\Database\Query\Processors\MySqlProcessor;
1275+
return new Builder(m::mock('Illuminate\Database\ConnectionInterface'), $grammar, $processor);
1276+
}
1277+
12561278
}

0 commit comments

Comments
 (0)