Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - cakephp datasource Call to undefined method

I've created a simple datasource:

// app/Model/Datasource/FeedSource.php

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

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

in my database.php:

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

And in Feeda model:

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

in list controller:

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

But, it returns a fatal error:

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

How to resolve it?

Thanks...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps you meant DboSource instead of DataSource.

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

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

Edit: 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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...