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

Commit

Permalink
Merge pull request #56 from tomlankhorst/master
Browse files Browse the repository at this point in the history
Use table prefixes only for raw SQL
  • Loading branch information
msonowal authored Dec 13, 2017
2 parents 506cff6 + 7b8d606 commit ba46b6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Services/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function createOrUpdateIndex()
protected function createIndex()
{
$indexName = $this->modelService->indexName;
$tableName = $this->modelService->tableName;
$tableName = $this->modelService->tablePrefixedName;
$indexFields = implode(',', array_map(function($indexField) {
return "`$indexField`";
}, $this->modelService->getFullTextIndexFields()));
Expand All @@ -84,7 +84,7 @@ protected function createIndex()

protected function indexAlreadyExists()
{
$tableName = $this->modelService->tableName;
$tableName = $this->modelService->tablePrefixedName;
$indexName = $this->modelService->indexName;

return !empty(DB::connection($this->modelService->connectionName)->
Expand All @@ -102,7 +102,7 @@ protected function indexNeedsUpdate()
protected function getIndexFields()
{
$indexName = $this->modelService->indexName;
$tableName = $this->modelService->tableName;
$tableName = $this->modelService->tablePrefixedName;

$index = DB::connection($this->modelService->connectionName)->
select("SHOW INDEX FROM $tableName WHERE Key_name = ?", [$indexName]);
Expand Down Expand Up @@ -131,7 +131,7 @@ protected function updateIndex()
public function dropIndex()
{
$indexName = $this->modelService->indexName;
$tableName = $this->modelService->tableName;
$tableName = $this->modelService->tablePrefixedName;

if ($this->indexAlreadyExists()) {
DB::connection($this->modelService->connectionName)
Expand Down
12 changes: 10 additions & 2 deletions src/Services/ModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class ModelService

public $connectionName;

public $tablePrefix;

public $tableName;

public $tablePrefixedName;

public $indexName;

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

$this->tableName = config("database.connections.$this->connectionName.prefix", '').$modelInstance->getTable();
$this->tablePrefix = config("database.connections.$this->connectionName.prefix", '');

$this->tableName = $modelInstance->getTable();

$this->tablePrefixedName = $this->tablePrefix.$this->tableName;

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

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

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

if (!isset($column[0])) {
Expand Down

0 comments on commit ba46b6e

Please sign in to comment.