Skip to content

Commit

Permalink
get the "users" table name from the config model
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr committed Sep 27, 2015
1 parent 4377370 commit 2691d79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
55 changes: 44 additions & 11 deletions src/Cmgmyr/Messenger/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Support\Facades\Config;

class Thread extends Eloquent
{
Expand All @@ -30,6 +31,13 @@ class Thread extends Eloquent
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at'];

/**
* "Users" table name to use for manual queries
*
* @var string|null
*/
private $usersTable = null;

/**
* Messages relationship
*
Expand Down Expand Up @@ -235,16 +243,16 @@ public function participantsString($userId=null, $columns=['name'])
{
$selectString = $this->createSelectString($columns);

$participantNames = $this->getConnection()->table('users')
->join('participants', 'users.id', '=', 'participants.user_id')
$participantNames = $this->getConnection()->table($this->getUsersTable())
->join('participants', $this->getUsersTable() . '.id', '=', 'participants.user_id')
->where('participants.thread_id', $this->id)
->select($this->getConnection()->raw($selectString));

if ($userId !== null) {
$participantNames->where('users.id', '!=', $userId);
$participantNames->where($this->getUsersTable() . '.id', '!=', $userId);
}

$userNames = $participantNames->lists('users.name');
$userNames = $participantNames->lists($this->getUsersTable() . '.name');

return implode(', ', $userNames);
}
Expand All @@ -271,25 +279,50 @@ public function hasParticipant($userId)
* @param $columns
* @return string
*/
private function createSelectString($columns)
protected function createSelectString($columns)
{
$dbDriver = $this->getConnection()->getDriverName();

switch ($dbDriver) {
case 'pgsql':
case 'sqlite':
$columnString = implode(" || ' ' || " . $this->getConnection()->getTablePrefix() . "users.", $columns);
$selectString = "(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
$columnString = implode(" || ' ' || " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
$selectString = "(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
break;
case 'sqlsrv':
$columnString = implode(" + ' ' + " . $this->getConnection()->getTablePrefix() . "users.", $columns);
$selectString = "(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
$columnString = implode(" + ' ' + " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
$selectString = "(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
break;
default:
$columnString = implode(", ' ', " . $this->getConnection()->getTablePrefix() . "users.", $columns);
$selectString = "concat(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
$columnString = implode(", ' ', " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
$selectString = "concat(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
}

return $selectString;
}

/**
* Sets the "users" table name
*
* @param $tableName
*/
public function setUsersTable($tableName)
{
$this->usersTable = $tableName;
}

/**
* Returns the "users" table name to use in manual queries
*
* @return string
*/
private function getUsersTable()
{
if ($this->usersTable !== null) {
return $this->usersTable;
}

$userModel = Config::get('messenger.user_model');
return $this->usersTable = (new $userModel)->getTable();
}
}
8 changes: 6 additions & 2 deletions tests/EloquentThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Carbon\Carbon;
use Cmgmyr\Messenger\Models\Thread;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Config;
use ReflectionClass;

class EloquentThreadTest extends TestCase
Expand Down Expand Up @@ -234,20 +235,23 @@ public function it_should_generate_participant_select_string()
{
$method = self::getMethod('createSelectString');
$thread = new Thread();
$tableName = 'users';
$thread->setUsersTable($tableName);

$columns = ['name'];
$select = $method->invokeArgs($thread, [$columns]);
$this->assertEquals("(" . Eloquent::getConnectionResolver()->getTablePrefix() . "users.name) as name", $select);
$this->assertEquals("(" . Eloquent::getConnectionResolver()->getTablePrefix() . $tableName . ".name) as name", $select);

$columns = ['name', 'email'];
$select = $method->invokeArgs($thread, [$columns]);
$this->assertEquals("(" . Eloquent::getConnectionResolver()->getTablePrefix() . "users.name || ' ' || " . Eloquent::getConnectionResolver()->getTablePrefix() . "users.email) as name", $select);
$this->assertEquals("(" . Eloquent::getConnectionResolver()->getTablePrefix() . $tableName . ".name || ' ' || " . Eloquent::getConnectionResolver()->getTablePrefix() . $tableName . ".email) as name", $select);
}

/** @test */
public function it_should_get_participants_string()
{
$thread = $this->faktory->create('thread');
$thread->setUsersTable('users');

$participant_1 = $this->faktory->build('participant');
$participant_2 = $this->faktory->build('participant', ['user_id' => 2]);
Expand Down

0 comments on commit 2691d79

Please sign in to comment.