| 1 | <?php |
||
| 17 | trait Accessor { |
||
| 18 | /** |
||
| 19 | * Link to db object |
||
| 20 | * @var false|_Abstract |
||
| 21 | */ |
||
| 22 | private $_db = false; |
||
| 23 | /** |
||
| 24 | * Link to primary db object |
||
| 25 | * @var false|_Abstract |
||
| 26 | */ |
||
| 27 | private $_db_prime = false; |
||
| 28 | /** |
||
| 29 | * Returns link to the object of db for reading (can be mirror of main DB) |
||
| 30 | * |
||
| 31 | * @return _Abstract |
||
| 32 | */ |
||
| 33 | function db () { |
||
| 34 | if (is_object($this->_db)) { |
||
| 35 | return $this->_db; |
||
| 36 | } |
||
| 37 | if (is_object($this->_db_prime)) { |
||
| 38 | return $this->_db = $this->_db_prime; |
||
| 39 | } |
||
| 40 | /** |
||
| 41 | * Save reference for faster access |
||
| 42 | */ |
||
| 43 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
| 44 | $this->_db = DB::instance()->db($this->cdb()); |
||
| 45 | return $this->_db; |
||
| 46 | } |
||
| 47 | /** |
||
| 48 | * Returns link to the object of db for writing (always main DB) |
||
| 49 | * |
||
| 50 | * @return _Abstract |
||
| 51 | */ |
||
| 52 | function db_prime () { |
||
| 53 | if (is_object($this->_db_prime)) { |
||
| 54 | return $this->_db_prime; |
||
| 55 | } |
||
| 56 | /** |
||
| 57 | * Save reference for faster access |
||
| 58 | */ |
||
| 59 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
| 60 | $this->_db_prime = DB::instance()->db_prime($this->cdb()); |
||
| 61 | return $this->_db_prime; |
||
| 62 | } |
||
| 63 | /** |
||
| 64 | * Returns database index |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | abstract protected function cdb (); |
||
| 69 | } |
||
| 70 |