Skip to content

Commit

Permalink
add methods for indicating the write connection should be used
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 3, 2021
1 parent dc2f0bb commit 94dbf76
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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?
*
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 94dbf76

Please sign in to comment.