Skip to content

Commit 5bc56f7

Browse files
committed
get the "users" table name from the config model
1 parent b718f82 commit 5bc56f7

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/Cmgmyr/Messenger/Models/Thread.php

+44-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Database\Eloquent\Model as Eloquent;
55
use Illuminate\Database\Eloquent\ModelNotFoundException;
66
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Support\Facades\Config;
78

89
class Thread extends Eloquent
910
{
@@ -30,6 +31,13 @@ class Thread extends Eloquent
3031
*/
3132
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
3233

34+
/**
35+
* "Users" table name to use for manual queries
36+
*
37+
* @var string|null
38+
*/
39+
private $usersTable = null;
40+
3341
/**
3442
* Messages relationship
3543
*
@@ -235,16 +243,16 @@ public function participantsString($userId=null, $columns=['name'])
235243
{
236244
$selectString = $this->createSelectString($columns);
237245

238-
$participantNames = $this->getConnection()->table('users')
239-
->join('participants', 'users.id', '=', 'participants.user_id')
246+
$participantNames = $this->getConnection()->table($this->getUsersTable())
247+
->join('participants', $this->getUsersTable() . '.id', '=', 'participants.user_id')
240248
->where('participants.thread_id', $this->id)
241249
->select($this->getConnection()->raw($selectString));
242250

243251
if ($userId !== null) {
244-
$participantNames->where('users.id', '!=', $userId);
252+
$participantNames->where($this->getUsersTable() . '.id', '!=', $userId);
245253
}
246254

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

249257
return implode(', ', $userNames);
250258
}
@@ -271,25 +279,50 @@ public function hasParticipant($userId)
271279
* @param $columns
272280
* @return string
273281
*/
274-
private function createSelectString($columns)
282+
protected function createSelectString($columns)
275283
{
276284
$dbDriver = $this->getConnection()->getDriverName();
277285

278286
switch ($dbDriver) {
279287
case 'pgsql':
280288
case 'sqlite':
281-
$columnString = implode(" || ' ' || " . $this->getConnection()->getTablePrefix() . "users.", $columns);
282-
$selectString = "(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
289+
$columnString = implode(" || ' ' || " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
290+
$selectString = "(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
283291
break;
284292
case 'sqlsrv':
285-
$columnString = implode(" + ' ' + " . $this->getConnection()->getTablePrefix() . "users.", $columns);
286-
$selectString = "(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
293+
$columnString = implode(" + ' ' + " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
294+
$selectString = "(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
287295
break;
288296
default:
289-
$columnString = implode(", ' ', " . $this->getConnection()->getTablePrefix() . "users.", $columns);
290-
$selectString = "concat(" . $this->getConnection()->getTablePrefix() . "users." . $columnString . ") as name";
297+
$columnString = implode(", ' ', " . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . ".", $columns);
298+
$selectString = "concat(" . $this->getConnection()->getTablePrefix() . $this->getUsersTable() . "." . $columnString . ") as name";
291299
}
292300

293301
return $selectString;
294302
}
303+
304+
/**
305+
* Sets the "users" table name
306+
*
307+
* @param $tableName
308+
*/
309+
public function setUsersTable($tableName)
310+
{
311+
$this->usersTable = $tableName;
312+
}
313+
314+
/**
315+
* Returns the "users" table name to use in manual queries
316+
*
317+
* @return string
318+
*/
319+
private function getUsersTable()
320+
{
321+
if ($this->usersTable !== null) {
322+
return $this->usersTable;
323+
}
324+
325+
$userModel = Config::get('messenger.user_model');
326+
return $this->usersTable = (new $userModel)->getTable();
327+
}
295328
}

tests/EloquentThreadTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Carbon\Carbon;
44
use Cmgmyr\Messenger\Models\Thread;
55
use Illuminate\Database\Eloquent\Model as Eloquent;
6+
use Illuminate\Support\Facades\Config;
67
use ReflectionClass;
78

89
class EloquentThreadTest extends TestCase
@@ -234,20 +235,23 @@ public function it_should_generate_participant_select_string()
234235
{
235236
$method = self::getMethod('createSelectString');
236237
$thread = new Thread();
238+
$tableName = 'users';
239+
$thread->setUsersTable($tableName);
237240

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

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

247250
/** @test */
248251
public function it_should_get_participants_string()
249252
{
250253
$thread = $this->faktory->create('thread');
254+
$thread->setUsersTable('users');
251255

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

0 commit comments

Comments
 (0)