本文介紹了cakephp 數(shù)據(jù)源調(diào)用未定義的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我創(chuàng)建了一個簡單的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
模型中:
class Feeda extends AppModel {
public $useTable = false;
public $useDbConfig = 'feed';
}
在list
控制器中:
$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 方法,它調(diào)用$this->getDataSource()->query($method, $params, $this);
來源你需要自己實現(xiàn)這個.
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);
}
}
}
這篇關(guān)于cakephp 數(shù)據(jù)源調(diào)用未定義的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!