From 94dbf768fa46917cb012a05b38cbc889dbd2e8a0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 3 Jun 2021 14:54:13 -0500 Subject: [PATCH] add methods for indicating the write connection should be used --- src/Illuminate/Database/Connection.php | 35 ++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 71efec09dfca..24bd64e375ae 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -129,10 +129,17 @@ class Connection implements ConnectionInterface /** * Indicates if changes have been made to the database. * - * @var int + * @var bool */ protected $recordsModified = false; + /** + * Indicates if the connection should use the "write" PDO connection. + * + * @var bool + */ + protected $readOnWriteConnection = false; + /** * All of the queries run against the connection. * @@ -861,6 +868,16 @@ public function raw($value) return new Expression($value); } + /** + * Determine if the database connection has modified any database records. + * + * @return bool + */ + public function hasModifiedRecords() + { + return $this->recordsModified; + } + /** * Indicate if any records have been modified. * @@ -884,6 +901,19 @@ public function forgetRecordModificationState() $this->recordsModified = false; } + /** + * Indicate that the connection should use the write PDO connection for reads. + * + * @param bool $value + * @return $this + */ + public function useWriteConnectionWhenReading($value = true) + { + $this->readOnWriteConnection = $value; + + return $this; + } + /** * Is Doctrine available? * @@ -980,7 +1010,8 @@ public function getReadPdo() return $this->getPdo(); } - if ($this->recordsModified && $this->getConfig('sticky')) { + if ($this->readOnWriteConnection || + ($this->recordsModified && $this->getConfig('sticky'))) { return $this->getPdo(); }