Last active
December 27, 2015 17:18
-
-
Save sobanvuex/7360711 to your computer and use it in GitHub Desktop.
Lithium Model extension to support master/slave write/read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\extensions\data; | |
use lithium\data\Connections; | |
/** | |
* Description of Model | |
* | |
* @author Alex Soban | |
*/ | |
class Model extends \lithium\data\Model { | |
/** | |
* @see lithium\data\Model::find() | |
*/ | |
public static function find($type, array $options = array()) { | |
static::_connection('slave', $options); | |
return parent::find($type, $options); | |
} | |
/** | |
* @see lithium\data\Model::save() | |
*/ | |
public function save($entity, $data = null, array $options = array()) { | |
static::_connection('master', $options); | |
return parent::save($entity, $data, $options); | |
} | |
/** | |
* Select the best available connection for this call | |
* | |
* $options['connection'] -> master/slave -> default | |
* | |
* @see lithium\data\Connections::get() | |
* @param string $connection | |
* @param array $options | |
* @return void | |
*/ | |
protected static function _connection($connection = false, array $options = array()) { | |
$connections = Connections::get(); | |
$current = static::_object()->meta('connection'); | |
if ( | |
!empty($options['connection']) && | |
!empty($connections[$options['connection']]) && | |
$options['connection'] !== $current | |
) { | |
static::_object()->meta('connection', $options['connection']); | |
} elseif ( | |
$connection && | |
!empty($connections[$connection]) && | |
$connection !== $current | |
) { | |
static::_object()->meta('connection', $connection); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment