我是CakePHP的新手,我还在学习基础知识,通过在一个实时项目中工作,并在必要时从CakePHP文档中获取帮助。目前,我遇到了以下问题:我最近更改了我的数据库表名和结构,因此我被迫更改了我的视图,控制器和模型名称。更改名称后,每当我运行index.ctp页面时,都会收到以下错误:
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_loan_distributions
,现在已重命名为home_loan_distributions_details
。
我之前的控制器名称为home_loan_distributions_controller.php
,当前名称为home_loan_distributions_details_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));
}
我的模型之前被命名为home_loan_distribution.php
,现在名为home_loan_distribution_detail.php
。代码:
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));
每当我注释掉这一行时,我就会停止获取上面提到的错误消息并加载我的视图页面(尽管仍然缺少一些数据 - 因为我需要在我的视图中显示那些与Branch相关的数据。) 我无法弄清楚我的问题究竟是什么,但至少我知道它在哪里。我需要有人来指出它。
我的CakePHP版本是1.2.5,PHP版本 - 5.2
答案 0 :(得分:0)
模型没有函数read
。如果你想找到一些模型数据,那么试试 -
$this->set('BranchDetailInformation', $this->HomeLoanDistributionDetail->Branch->find('all', $consitions);
$conditions
将是您要提供的所有要求的数组。有关详细信息,请参阅docs。
答案 1 :(得分:0)
显然,问题似乎与我的模型中使用的'className' => 'Branch'
数组的Branch
元素有关,因为stdClass::read()
方法与类而不是模型相关。但我发现问题出在其他地方。此错误是该问题的一部分,但它本身并不是实际问题。
今天早上我发现我的模型名称是HomeLoanDistributionDetail
,但我的表名是home_loan_distributions_details
(因为其他人更改了表名)。 CakePHP约定要求相应的表名为复数,模型类名称为singular和CamelCased。
模型类名称是单数和 CamelCased 。 Person,BigPerson和ReallyBigPerson都是传统模型名称的例子。
与CakePHP模型对应的表名称是复数和强调。上述模型的基础表分别是people,big_people和really_big_people。
考虑到上述惯例,我只需将我的模型类名称从HomeLoanDistributionDetail
重命名为HomeLoanDistributionsDetail
,以便与表名home_loan_distributions_details
匹配。另外,我必须将模型文件名从home_loan_distribution_detail.php
更改为home_loan_distributions_detail.php
。
在这样做之后,我停止了错误,我成功地从表中检索数据并查看它。