ConnectionManager getDataSource undefined method
使用cakephp 2.3.8,我正在尝试建立与我在另一个数据源中创建的自定义榻榻米数据库数据源的连接。通过模型加载时,我尝试加载的数据源在站点的其余部分上正常运行。我想使用连接管理器加载此数据源。这是我到目前为止的内容:
database.php文件:
1 2 3 4 5 6 7 8 9 10 11 | public $queriesCB = array( 'datasource' => 'CouchbaseSource', 'username' => 'queries', 'password' => '', 'bucket' => 'queries', 'prefix' => 'q_', 'expiry' => '1814400', //3 Weeks 'autoConnect' => true, 'database' => NULL , 'persistent' => false ); |
在我的其他数据源中,尝试加载沙发数据库数据源
1 | $db = ConnectionManager::getDataSource("queriesCB"); |
调试$ db时得到以下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | object(CouchbaseSource) { description => 'Couchbase DataSource' conObject => object(Couchbase) { [private] _handle => resource } config => array( 'password' => '*****', 'database' => '*****', 'prefix' => '*****', 'datasource' => 'CouchbaseSource', 'username' => 'queries', 'bucket' => 'queries', 'expiry' => '1814400', 'autoConnect' => true, 'persistent' => false ) prefix => 'q__' connected => false cacheSources => true configKeyName => 'queriesCB' [protected] _baseConfig => array() [protected] _descriptions => array() [protected] _sources => null [protected] _transactionStarted => false } |
现在,当我尝试调用此命令时,出现错误:
1 2 | $db = ConnectionManager::getDataSource("queriesCB"); $db->Get('test'); |
错误:调用未定义的方法CouchbaseSource :: Get()。
这是一个自定义方法,所有数据源都是自定义的,可以与沙发床一起使用,并且Get方法可以正常运行。我如何在cakephp中设置错误的连接?
编辑:
我刚刚使用默认的数据库配置测试了它到mysql数据库,它也失败了。现在的问题是初始化新数据源的最佳方法是什么?我是否必须加载附加了该数据源的模型?示例:是否有一个以数据源为ouchbase的couchbase模型?
编辑:这是一些数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | class CouchbaseSource extends DataSource { public $description = 'Couchbase DataSource'; public $conObject = NULL; public $config = NULL; public $prefix = NULL; public function __construct($config = array()){ // If no configuration is set we use the default $this->config = $config; // Setup the cache string that is used when building the string $this->prefix = (isset($this->config['prefix']) ? $this->config['prefix']."_" :""); if ($this->config['autoConnect']) { $this->connect(); } } public function connect() { if ($this->conObject !== true) { try { $this->conObject = new Couchbase("127.0.0.1:8091", $this->config['username'], $this->config['password'], $this->config['bucket'], $this->config['persistent']); } catch (Exception $e) { throw new MissingConnectionException(array('class' => $e->getMessage())); } } return $this->conObject; } public function query($method, $params, $object) { // If not connected... reconnect! if(!$this->conObject) { $this->connect(); } $apiMethod = $this->__methodToClass($method); if (!method_exists($this, $apiMethod)) { throw new NotFoundException("Class '{$apiMethod}' was not found"); } else { return call_user_func_array(array($this, $apiMethod), $params); } } private function __methodToClass($method) { return 'CB' . strtolower(Inflector::camelize($method)); } public function describe(&$Model) { return $this->description; } ///////////////////////////////////////////////// // Query Methods ///////////////////////////////////////////////// public function CBadd($key = NULL, $value = NULL, $expiry = NULL, $persisto = NULL, $replicateto = NULL) { return $this->conObject->add($key, $value, $expiry, $persisto, $replicateto); } |
解决方案是直接从couchbasesource调用查询方法。考虑到我设置数据源的方式,与通过模型链接数据源不同,查询信息不会自动传递。我的解决方法是简单地使用它。
1 2 3 | App::uses('ConnectionManager', 'Model'); $db = ConnectionManager::getDataSource("queriesCB"); debug($db->query('Get', array('test'))); |