Welcome to OStack Knowledge Sharing 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...