GermaniaKG /
Users
| 1 | <?php |
||
| 2 | namespace Germania\Users; |
||
| 3 | |||
| 4 | class PdoAllUsers extends Users implements UsersInterface |
||
| 5 | { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | public $table = 'users'; |
||
| 11 | |||
| 12 | |||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | public $users = array(); |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * @param PDO $pdo |
||
| 21 | * @param UserAbstract $user Optional: User template object |
||
| 22 | * @param string $table Optional: Users table name |
||
| 23 | */ |
||
| 24 | 20 | public function __construct( \PDO $pdo, UserAbstract $user = null, $table = null ) |
|
| 25 | { |
||
| 26 | 20 | $this->table = $table ?: $this->table; |
|
| 27 | |||
| 28 | // ID is listed twice here in order to use it with FETCH_UNIQUE as array key |
||
| 29 | $sql = "SELECT |
||
| 30 | id, |
||
| 31 | id AS id, |
||
| 32 | user_first_name AS first_name, |
||
| 33 | user_last_name AS last_name, |
||
| 34 | user_login_name AS login_name, |
||
| 35 | user_display_name AS display_name, |
||
| 36 | user_email AS email, |
||
| 37 | api_key AS api_key, |
||
| 38 | is_active AS is_active, |
||
| 39 | created AS created, |
||
| 40 | updated AS updated |
||
| 41 | |||
| 42 | 20 | FROM {$this->table}"; |
|
| 43 | |||
| 44 | 20 | $stmt = $pdo->prepare( $sql ); |
|
| 45 | |||
| 46 | 20 | $stmt->setFetchMode( \PDO::FETCH_CLASS, $user ? get_class($user) : User::class ); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 47 | |||
| 48 | 20 | if (!$stmt->execute()): |
|
| 49 | 10 | throw new \RuntimeException("Could not retrieve Users from database"); |
|
| 50 | endif; |
||
| 51 | |||
| 52 | 10 | $this->users = $stmt->fetchAll(\PDO::FETCH_UNIQUE); |
|
| 53 | 10 | } |
|
| 54 | |||
| 55 | } |
||
| 56 | |||
| 57 |