I'm currently in the process of replacing a self-made hack-y MVC approach's Model component with php-activerecord. I have a separate page that displays in the event that the database server goes down, and that page used to display a user badge with a guest avatar, name and role. The code is as follows:
if (Auth::$signed_in)
echo Auth::$user->getAvatarWrap();
else echo (new \App\Models\User([
'name' => 'Guest',
'role' => 'guest',
'avatar_url' => GUEST_AVATAR
]))->getAvatarWrap();
Auth::$signed_in
is set to true
when a user is logged in, which - in case of a DB outage - is impossible, so the else
branch executes with the predefined data.
Pre-ActiveRecord this would simply add the properties to the object and the call to the getAvatarWrap
method would execute without any issues. Now, with the model being controlled by ActiveRecord, a set of additional calls take place for whatever reason. This could be because the model has relations defined, but I'm not sure.
ActiveRecord\DatabaseException: PDOException: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432? in /var/www/vendor/php-activerecord/php-activerecord/lib/Connection.php:260
Stack trace:
#0 /var/www/vendor/php-activerecord/php-activerecord/lib/Connection.php(260): PDO->__construct('pgsql:host=loca...', '<username>', '<password>', Array)
#1 /var/www/vendor/php-activerecord/php-activerecord/lib/Connection.php(122): ActiveRecord\Connection->__construct(Object(stdClass))
#2 /var/www/vendor/php-activerecord/php-activerecord/lib/ConnectionManager.php(33): ActiveRecord\Connection::instance('pgsql://databas...')
#3 /var/www/vendor/php-activerecord/php-activerecord/lib/Table.php(114): ActiveRecord\ConnectionManager::get_connection('pgsql')
#4 /var/www/vendor/php-activerecord/php-activerecord/lib/Table.php(90): ActiveRecord\Table->reestablish_connection(false)
#5 /var/www/vendor/php-activerecord/php-activerecord/lib/Table.php(71): ActiveRecord\Table->__construct('App\\Models\\User')
#6 /var/www/vendor/php-activerecord/php-activerecord/lib/Model.php(765): ActiveRecord\Table::load('App\\Models\\User')
#7 /var/www/vendor/php-activerecord/php-activerecord/lib/Model.php(271): ActiveRecord\Model::table()
#8 /var/www/includes/views/_sidebar.php(20): ActiveRecord\Model->__construct(Array)
#9 /var/www/includes/views/_layout.php(148): include('/var/www...')
#10 /var/www/includes/views/fatalerr.php(46): require('/var/www...')
#11 /var/www/includes/init.php(32): require('/var/www...')
#12 /var/www/includes/do.php(3): require('/var/www...')
#13 /var/www/public/index.php(1): require('/var/www...')
#14 {main} in /var/www/vendor/php-activerecord/php-activerecord/lib/Connection.php on line 262
How do I tell ActiveRecord to stop looking for a connection when I know for a fact that it won't find one, and that it should be content with the data I'm feeding it? Do I have to wave goodbye to using any of my models during an outage?