cakephp datasource调用未定义的方法 [英] cakephp datasource Call to undefined method

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

问题描述

我创建了一个简单的数据源

// app/Model/Datasource/FeedSource.php

App::uses('DataSource', 'Model/Datasource');

class FeedSource extends DataSource {
    public function abcd() {
        echo 'Hello World!';
    }
}

在我的 database.php

public $feed = array(
    'datasource' => 'FeedSource'
);

并且在 Feeda b

And in Feeda model:

class Feeda extends AppModel {
    public $useTable = false;
    public $useDbConfig = 'feed';
}

$this->loadModel('Feeda');
$this->Feeda->abcd();

但是,它返回一个致命错误:

But, it returns a fatal error:

Error: Call to undefined method FeedSource::query()

如何解决?

谢谢...

推荐答案

也许你的意思是 DboSource 而不是 DataSource

Perhaps you meant DboSource instead of DataSource.

DataSource没有方法查询,DboSource。更新您的代码如下:

DataSource has no method query, DboSource does. Update your code to look like:

App::uses('DboSource', 'Model/Datasource');
class FeedSource extends DboSource {}

编辑:看起来不是问题。在 Model 中有一个奇怪的__call方法调用
$ this-> getDataSource() - > query $ params,$ this); 您需要

Looks like that is not the issue. In the Model there is a magic __call method which calls $this->getDataSource()->query($method, $params, $this); Source You need to implement this yourself.

class FeedSource extends DataSource {
    public function abcd() {
        echo 'Hello World!';
    }

    public function query($method, $params, $Model) {
        // you may customize this to your needs.
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        }
    }
}

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

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