【发布时间】:2011-07-11 16:53:16
【问题描述】:
这很奇怪。
我有一个用于开发应用程序的本地服务器。我开发的一个产品评论应用程序可以完美运行,并利用 Cake 的关联建模($hasMany、$belongsTo 等)。
将此应用程序推送到生产服务器后,它失败了。给我一条错误消息:
Notice (8): Undefined property: AppModel::$Product [APP/controllers/reviews_controller.php, line 46]
ReviewsController::home() - APP/controllers/reviews_controller.php, line 46
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
我已经 debug()'d $this 并且它清楚地表明,当本地服务器正在加载相关模型时,生产服务器没有。数据库是镜像副本(从字面上看,生产服务器是从 dev db 导入的),我可以手动加载模型,这告诉我它可以很好地连接到数据库。
到底发生了什么?
更新
来自生产服务器的 sql 查询是这样的:
SELECT `Review`.`id`, `Review`.`title`, `Review`.`product_id`, `Review`.`score`, `Review`.`submitted`, `Review`.`reviewed`, `Review`.`right`, `Review`.`wrong`, `Review`.`user_id`, `Review`.`goals`
FROM `reviews`
AS `Review`
WHERE 1 = 1
ORDER BY `Review`.`submitted` desc LIMIT 10
来自开发服务器的 sql 查询是这样的:
SELECT `Review`.`id`, `Review`.`title`, `Review`.`product_id`, `Review`.`score`, `Review`.`submitted`, `Review`.`reviewed`, `Review`.`right`, `Review`.`wrong`, `Review`.`user_id`, `Review`.`goals`, `User`.`id`, `User`.`username`, `Product`.`id`, `Product`.`name`
FROM `reviews`
AS `Review`
LEFT JOIN `users` AS `User` ON (`Review`.`user_id` = `User`.`id`)
LEFT JOIN `products` AS `Product` ON (`Review`.`product_id` = `Product`.`id`)
WHERE 1 = 1
ORDER BY `Review`.`submitted` desc LIMIT 10
更新 2
这是错误指向的一些代码:
$title = $this->Review->Product->find( 'first', array( 'fields' => array( 'Product.name' ), 'conditions' => array( 'Product.id' => $filter ) ) );
更新 3
<?php
class Review extends AppModel {
var $name = 'Review';
var $displayField = 'title';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
【问题讨论】:
-
你清除缓存(即tmp文件夹)了吗?
-
如果它发生在一个地方而不是另一个地方,这听起来像是部署中出了问题。我不知道 Cake 会忽略关联的原因。
-
好吧,如果我至少能弄清楚从哪里着手解决这个问题,就更容易找出哪个文件明显失败了。
-
你能把调用产品模型的代码贴出来吗?
-
您是如何定义 Review 模型中的关系的?似乎蛋糕忽略了该模型的所有关系。你能测试一下它是否发生在其他模型上吗?
标签: cakephp associations models