Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit ba46b6e

Browse files
authored
Merge pull request #56 from tomlankhorst/master
Use table prefixes only for raw SQL
2 parents 506cff6 + 7b8d606 commit ba46b6e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Services/IndexService.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function createOrUpdateIndex()
6767
protected function createIndex()
6868
{
6969
$indexName = $this->modelService->indexName;
70-
$tableName = $this->modelService->tableName;
70+
$tableName = $this->modelService->tablePrefixedName;
7171
$indexFields = implode(',', array_map(function($indexField) {
7272
return "`$indexField`";
7373
}, $this->modelService->getFullTextIndexFields()));
@@ -84,7 +84,7 @@ protected function createIndex()
8484

8585
protected function indexAlreadyExists()
8686
{
87-
$tableName = $this->modelService->tableName;
87+
$tableName = $this->modelService->tablePrefixedName;
8888
$indexName = $this->modelService->indexName;
8989

9090
return !empty(DB::connection($this->modelService->connectionName)->
@@ -102,7 +102,7 @@ protected function indexNeedsUpdate()
102102
protected function getIndexFields()
103103
{
104104
$indexName = $this->modelService->indexName;
105-
$tableName = $this->modelService->tableName;
105+
$tableName = $this->modelService->tablePrefixedName;
106106

107107
$index = DB::connection($this->modelService->connectionName)->
108108
select("SHOW INDEX FROM $tableName WHERE Key_name = ?", [$indexName]);
@@ -131,7 +131,7 @@ protected function updateIndex()
131131
public function dropIndex()
132132
{
133133
$indexName = $this->modelService->indexName;
134-
$tableName = $this->modelService->tableName;
134+
$tableName = $this->modelService->tablePrefixedName;
135135

136136
if ($this->indexAlreadyExists()) {
137137
DB::connection($this->modelService->connectionName)

src/Services/ModelService.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ class ModelService
1010

1111
public $connectionName;
1212

13+
public $tablePrefix;
14+
1315
public $tableName;
1416

17+
public $tablePrefixedName;
18+
1519
public $indexName;
1620

1721
protected $fullTextIndexTypes = ['VARCHAR', 'TEXT', 'CHAR'];
@@ -25,7 +29,11 @@ public function setModel($model)
2529
$this->connectionName = $modelInstance->getConnectionName() !== null ?
2630
$modelInstance->getConnectionName() : config('database.default');
2731

28-
$this->tableName = config("database.connections.$this->connectionName.prefix", '').$modelInstance->getTable();
32+
$this->tablePrefix = config("database.connections.$this->connectionName.prefix", '');
33+
34+
$this->tableName = $modelInstance->getTable();
35+
36+
$this->tablePrefixedName = $this->tablePrefix.$this->tableName;
2937

3038
$this->indexName = $modelInstance->searchableAs();
3139

@@ -40,7 +48,7 @@ public function getFullTextIndexFields()
4048
foreach ($searchableFields as $searchableField) {
4149

4250
//@TODO cache this.
43-
$sql = "SHOW FIELDS FROM $this->tableName where Field = ?";
51+
$sql = "SHOW FIELDS FROM $this->tablePrefixedName where Field = ?";
4452
$column = DB::connection($this->connectionName)->select($sql, [$searchableField]);
4553

4654
if (!isset($column[0])) {

0 commit comments

Comments
 (0)