CakePHP调用未定义的方法stdClass::read()错误
我是CakePHP新手,我仍然在学习基础知识,通过在一个实时项目中工作,并在必要时从CakePHP文档中获得帮助。目前,我遇到了以下问题:我最近更改了数据库表名和结构,因此我被迫更改视图、控制器和模型名。更改名称后,无论何时运行index.ctp页面,都会出现以下错误:CakePHP调用未定义的方法stdClass::read()错误,php,cakephp,Php,Cakephp,我是CakePHP新手,我仍然在学习基础知识,通过在一个实时项目中工作,并在必要时从CakePHP文档中获得帮助。目前,我遇到了以下问题:我最近更改了数据库表名和结构,因此我被迫更改视图、控制器和模型名。更改名称后,无论何时运行index.ctp页面,都会出现以下错误: Fatal error: Call to undefined method stdClass::read() in C:\wamp\www\sdb\app\controllers \home_loan_distributions
Fatal error: Call to undefined method stdClass::read() in C:\wamp\www\sdb\app\controllers
\home_loan_distributions_details_controller.php on line 32
以前,我的视图文件夹名为home\u loan\u distributions
,现在它被重命名为home\u loan\u distributions\u details
我以前的控制器名称是home\u loan\u distributions\u controller.php
,当前名称是home\u loan\u distributions\u details\u controller.php
。代码:
class HomeLoanDistributionsDetailsController extends AppController {
var $name = 'HomeLoanDistributionsDetails';
function index() {
$user = $this->Session->read('User');
$user_role = $user['User']['user_role_id'];
$actions = $this->Session->read('actions');
$display_actions = array();
foreach ($actions as $action) {
array_push($display_actions, $action['pm_controller_actions']['name']);
}
$this->set('display_actions', $display_actions);
$this->set('user_role', $user_role);
$branch_id = 18;
$this->set('branch_id', $branch_id);
$conditions = array('branch_id' => $branch_id);
$this->set('HomeLoanDistributionsDetails', $this->paginate($conditions));
$this->HomeLoanDistributionDetail->Branch->recursive = 0;
$this->set('BranchDetailInformation', $this->HomeLoanDistributionDetail->Branch->read(array('Branch.id', 'Branch.name', 'RegionalOffice.name', 'DistrictOffice.name', 'SubDistrictOffice.name', 'ClusterOffice.name'), $branch_id));
}
class HomeLoanDistributionDetail extends AppModel {
var $name = 'HomeLoanDistributionDetail';
var $actsAs = array('Logable' => array(
'userModel' => 'User',
'userKey' => 'user_id',
'change' => 'list', // options are 'list' or 'full'
'description_ids' => TRUE // options are TRUE or FALSE
));
var $validate = array(
'entry_date' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
),
'branch_id' => array('numeric'),
'customer_id' => array('numeric'),
'loan_amount' => array('numeric'),
'service_charge' => array('numeric'),
'security' => array('numeric'),
'loan_taken_term' => array('numeric'),
'purpose_id' => array('numeric'),
'installment_amount' => array('numeric'),
'installment_service_charge' => array('numeric'),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Branch' => array(
'className' => 'Branch',
'foreignKey' => 'branch_id',
'conditions' => '',
'fields' => 'id,name',
'order' => ''
)
);
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = 0;
$group = $fields = array('branch_id', 'entry_date');
$order = array('entry_date DESC');
$limit = 4;
$this->paginateCount($conditions);
return $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'recursive', 'group'));
}
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$recursive = 0;
$group = $fields = array('branch_id', 'entry_date');
$order = array('entry_date DESC');
$results = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive', 'group'));
return count($results);
}
}
我的模型以前名为home\u loan\u distribution.php
,现在名为home\u loan\u distribution\u detail.php
。代码:
class HomeLoanDistributionsDetailsController extends AppController {
var $name = 'HomeLoanDistributionsDetails';
function index() {
$user = $this->Session->read('User');
$user_role = $user['User']['user_role_id'];
$actions = $this->Session->read('actions');
$display_actions = array();
foreach ($actions as $action) {
array_push($display_actions, $action['pm_controller_actions']['name']);
}
$this->set('display_actions', $display_actions);
$this->set('user_role', $user_role);
$branch_id = 18;
$this->set('branch_id', $branch_id);
$conditions = array('branch_id' => $branch_id);
$this->set('HomeLoanDistributionsDetails', $this->paginate($conditions));
$this->HomeLoanDistributionDetail->Branch->recursive = 0;
$this->set('BranchDetailInformation', $this->HomeLoanDistributionDetail->Branch->read(array('Branch.id', 'Branch.name', 'RegionalOffice.name', 'DistrictOffice.name', 'SubDistrictOffice.name', 'ClusterOffice.name'), $branch_id));
}
class HomeLoanDistributionDetail extends AppModel {
var $name = 'HomeLoanDistributionDetail';
var $actsAs = array('Logable' => array(
'userModel' => 'User',
'userKey' => 'user_id',
'change' => 'list', // options are 'list' or 'full'
'description_ids' => TRUE // options are TRUE or FALSE
));
var $validate = array(
'entry_date' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
),
'branch_id' => array('numeric'),
'customer_id' => array('numeric'),
'loan_amount' => array('numeric'),
'service_charge' => array('numeric'),
'security' => array('numeric'),
'loan_taken_term' => array('numeric'),
'purpose_id' => array('numeric'),
'installment_amount' => array('numeric'),
'installment_service_charge' => array('numeric'),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Branch' => array(
'className' => 'Branch',
'foreignKey' => 'branch_id',
'conditions' => '',
'fields' => 'id,name',
'order' => ''
)
);
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = 0;
$group = $fields = array('branch_id', 'entry_date');
$order = array('entry_date DESC');
$limit = 4;
$this->paginateCount($conditions);
return $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'recursive', 'group'));
}
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$recursive = 0;
$group = $fields = array('branch_id', 'entry_date');
$order = array('entry_date DESC');
$results = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive', 'group'));
return count($results);
}
}
我的猜测是:可能是我在重命名所有东西时弄乱了命名约定。问题肯定在控制器的这一行内:
$this->set('BranchDetailInformation',
$this->HomeLoanDistributionDetail->Branch->read(array('Branch.id', 'Branch.name',
'RegionalOffice.name', 'DistrictOffice.name', 'SubDistrictOffice.name', 'ClusterOffice.name'),
$branch_id));
每当我注释掉这一行时,我就不再收到上面提到的错误消息并加载我的视图页面(尽管仍然缺少一些数据,因为我需要在视图中显示那些与分支相关的数据)
我不知道我的问题到底是什么,但至少我知道它在哪里。我需要有人来确定它
我的CakePHP版本是1.2.5,PHP版本是-5.2没有用于模型的函数
read
。如果要查找一些模型数据,请尝试-
$this->set('BranchDetailInformation', $this->HomeLoanDistributionDetail->Branch->find('all', $consitions);
$conditions
将是您想要提供的所有需求的数组。有关更多信息,请参阅。显然,问题似乎与我的模型中使用的分支数组的'className'=>'Branch'
元素有关,因为stdClass::read()
方法与类而不是模型相关。但我发现问题出在别处。这个错误是问题的一部分,但它本身并不是真正的问题
今天早上我发现我的模型名是HomeLoanDistributionDetail
,但我的表名是home\u loan\u distributions\u details
(因为其他人更改了表名)。CakePHP约定要求对应的表名为复数,模型类名为单数且大小写一致
引述:
模型类名称是单数的和CamelCased的。Person、BigPerson和ReallyBigPerson都是常规模型名称的示例
与CakePHP模型对应的表名是复数和下划线。上述模型的基础表分别是people、big_people和really_big_people
考虑到上述约定,我只需将模型类名从HomeLoandDistributionDetail
重命名为HomeLoandDistributionDetail
,以便与表名home\u loan\u distributions\u details
匹配。此外,我必须将模型文件名从home\u loan\u distribution\u detail.php
更改为home\u loan\u distributions\u detail.php
这样做之后,我停止了获取错误,并成功地从表中检索数据并查看它。$this->HomeLoanDistributionDetail->Branch
我认为Branch
在这里被标识为一个类,而不是一个模型。也许这就是为什么我可以使用read()
方法?类似的代码存在于项目的其他部分,我见过他们这样写,而且这些代码工作得很好<代码>分支机构
我想这里不是一个模型。你能添加那个代码吗<代码>分支
是模型的一个实例。您还定义了关系<代码>读取是一种会话
组件而非模型的方法。