ConnectionManager getDataSource未定义方法 [英] ConnectionManager getDataSource undefined method

查看:227
本文介绍了ConnectionManager getDataSource未定义方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用cakephp 2.3.8我试图建立一个连接到我在另一个数据源中创建的自定义couchbase数据源。数据源我通过模型加载时试图在网站的其余部分上正确加载函数。我想使用连接管理器来加载此数据源。这是我到目前为止:

Using cakephp 2.3.8 I am attempting to setup a connection to a custom couchbase datasource I have made within another datasource. The datasource I am attempting to load functions properly on the rest of the site when being loaded via a model. I want to use the connection manager to load this datasource. This is what I have so far:

database.php文件:

database.php file:

public $queriesCB = array(  
    'datasource' => 'CouchbaseSource',
    'username'      => 'queries',
    'password'   => '',
    'bucket'        => 'queries', 
    'prefix'    => 'q_',
    'expiry'    => '1814400', //3 Weeks
    'autoConnect' => true, 
    'database'      => NULL , 
    'persistent' => false
    );

在我尝试加载couchbase数据源的其他数据源中

Within my other datasource trying to load couchbase datasource

$db = ConnectionManager::getDataSource("queriesCB"); 

当我调试$ db时,我得到:

When I debug $db I get this:

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
}

现在当我尝试调用这个我得到一个错误:

Now when I try to call this I get a error:

$db = ConnectionManager::getDataSource("queriesCB"); 
$db->Get('test');

错误:调用未定义的方法CouchbaseSource :: Get()。

Error: Call to undefined method CouchbaseSource::Get().

这是一个自定义方法,所有数据源都是自定义的,最好与couchbase和Get方法正常工作。我如何在cakephp中设置此连接错误?

This is a custom method, the datasource is all custom to work best with couchbase and the Get method does function properly. How am I setting up this connection wrong in cakephp?

编辑:

数据库配置到一个mysql数据库,它也失败。现在的问题是什么是最好的方式来初始化一个新的数据源?我应该加载一个数据源附加的模型吗?示例:将数据源的couchbase模型作为couchbase?

I just tested it with the default database config to a mysql database, it fails as well. The question now would be what would be the best way to initialize a new datasource? should I have to load a model with that datasource attached? Example: have a couchbase model with the datasource as couchbase?

编辑:这里是一些数据源

Here is some of the datasource

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的查询方法。给定我设置数据源的方式,不像通过模型链接它,查询信息不会自动传递。我的方法是简单使用这个。

The solution is to directly call the query method from the couchbasesource. Given the way that I setup the datasource, unlike linking it through a model, the query information was not automatically passed. My go around was to simple use this.

App::uses('ConnectionManager', 'Model');
$db = ConnectionManager::getDataSource("queriesCB");
debug($db->query('Get', array('test')));

这篇关于ConnectionManager getDataSource未定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆