@@ -44,6 +44,8 @@ abstract class PdoRepository extends Repository
4444 */
4545 protected static $ defaultConnection = null ;
4646
47+ protected static $ readOnlyConnection = null ;
48+
4749 private static $ pdoParamAliasesUsed = [];
4850
4951 /**
@@ -81,11 +83,46 @@ public static function getDefaultConnection()
8183 $ databaseSettings = StemSettings::singleton ();
8284
8385 self ::$ defaultConnection = static ::getConnection ($ databaseSettings );
86+
87+ if ($ databaseSettings ->stickyWriteConnection ) {
88+ self ::$ readOnlyConnection = self ::$ defaultConnection ;
89+ }
8490 }
8591
8692 return self ::$ defaultConnection ;
8793 }
8894
95+ public static function getReadOnlyConnection ()
96+ {
97+ if (self ::$ readOnlyConnection === null ) {
98+ $ databaseSettings = StemSettings::singleton ();
99+
100+ if (
101+ // readonly port/host are different to default
102+ $ databaseSettings ->readOnlyHost !== $ databaseSettings ->host
103+ || $ databaseSettings ->readOnlyPort !== $ databaseSettings ->port
104+ ) {
105+ $ readOnlySettings = clone StemSettings::singleton ();
106+ $ readOnlyMap = [
107+ 'host ' => 'readOnlyHost ' ,
108+ 'port ' => 'readOnlyPort ' ,
109+ 'username ' => 'readOnlyUsername ' ,
110+ 'password ' => 'readOnlyPassword ' ,
111+ ];
112+ foreach ($ readOnlyMap as $ primaryProp => $ readOnlyProp ) {
113+ if ($ readOnlySettings ->$ readOnlyProp ) {
114+ $ readOnlySettings ->$ primaryProp = $ readOnlySettings ->$ readOnlyProp ;
115+ }
116+ }
117+ self ::$ readOnlyConnection = static ::getConnection ($ readOnlySettings );
118+ } else {
119+ self ::$ readOnlyConnection = self ::getDefaultConnection ();
120+ }
121+ }
122+
123+ return self ::$ readOnlyConnection ;
124+ }
125+
89126 /**
90127 * @param StemSettings $dbSettings
91128 * @return \PDO
@@ -104,6 +141,13 @@ public static function resetDefaultConnection()
104141 self ::$ defaultConnection = null ;
105142 }
106143
144+ /**
145+ * Discards the default connection.
146+ */
147+ public static function resetReadOnlyConnection ()
148+ {
149+ self ::$ readOnlyConnection = null ;
150+ }
107151
108152 /**
109153 * A collection of PDO objects for each active connection.
@@ -231,7 +275,11 @@ public static function executeInsertStatement($sql, $namedParameters = [], $conn
231275 */
232276 public static function returnSingleValue ($ statement , $ namedParameters = [], $ connection = null )
233277 {
234- $ statement = self ::executeStatement ($ statement , $ namedParameters , $ connection );
278+ $ statement = self ::executeStatement (
279+ $ statement ,
280+ $ namedParameters ,
281+ $ connection !== null ? $ connection : self ::getReadOnlyConnection ()
282+ );
235283
236284 return $ statement ->fetchColumn (0 );
237285 }
@@ -246,7 +294,11 @@ public static function returnSingleValue($statement, $namedParameters = [], $con
246294 */
247295 public static function returnFirstRow ($ statement , $ namedParameters = [], $ connection = null )
248296 {
249- $ statement = self ::executeStatement ($ statement , $ namedParameters , $ connection );
297+ $ statement = self ::executeStatement (
298+ $ statement ,
299+ $ namedParameters ,
300+ $ connection !== null ? $ connection : self ::getReadOnlyConnection ()
301+ );
250302
251303 return $ statement ->fetch (\PDO ::FETCH_ASSOC );
252304 }
0 commit comments