CakePHP 未在生产服务器上加载与模型相关的属性
这是一个奇怪的现象。
我有一个本地服务器,我在其上开发应用程序。我开发的产品评论应用程序可以完美地运行它,并利用 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
并且它清楚地显示,当本地服务器正在加载关联的模型时,生产服务器不是。数据库是镜像副本(从字面上看,生产服务器是从开发数据库导入的),我可以手动加载模型,这告诉我它连接到数据库的情况很好。
到底发生了什么事?
UPDATE
来自生产服务器的 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
UPDATE 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' => ''
)
);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了这个问题,对我来说,这是由于其中一个数据库表中缺少字段造成的。我会三次检查以确保两个数据库完全相同(尽管你说它们是......):请随意使用这个已有 7 年历史的应用程序来检查它们:D http://www.mysqldiff.org/
其他有此问题的人讨论了文件名问题,所有文件都应该小写,所以这可能是一些问题还要检查...
I had this problem, and for me it was due to a missing field in one of the database tables. I'd triple-check to make sure both DB's are exactly the same (although you said they were...): feel free to use this 7-year-old app to check them :D http://www.mysqldiff.org/
Other people with this issue talked about filename issues and that all files should be lowercased, so that may be something to check as well...
实际上 - 乍一看,可能值得使用 containable 来确保您的数据调用持续的。
如果您不想经历添加可容纳的麻烦(但我强烈建议您这样做 - 这是我最喜欢的 cakephp 功能之一),您可能需要设置 递归 在 find() 调用中只是为了确保加载关联的模型。
Actually - from a quick glance it might be worth using containable to make sure your data calls are consistent.
If you don't want to go through the hassle of adding containable (but I would urge you to do so - it is one my favourite features of cakephp), you may want to set recursive in your find() call just to make sure the associated models are loaded.
有没有办法不用通过ftp就可以查看服务器上的文件?我遇到了与此类似的问题,其中文件上的时间戳被弄乱,并且服务器不会更新文件。我必须删除这些文件,然后重新上传。你可能已经尝试过这个,但我只是想我会建议这种可能性。也许其中一些文件在服务器上已经过时。
祝你有美好的一天!
Do you have a way of looking at the files on the server without going through ftp? I had a problem similar to this where the timestamps were messed up on the files and the server would not update the file. I had to delete the files, and then re upload them. You may have already tried this but I just thought I would suggest the possibility. Maybe some of those files are outdated on the server.
Have a great day!
您可以粘贴评论模型、产品模型、AppModel、AppController 以及您收到错误的控制器吗?
该行:
注意(8):未定义的属性:AppModel::$Product [APP/controllers/reviews_controller.php,第 46 行]
似乎表明审阅模型正在加载 AppModel,而不是您想要的文件。在这种情况下,评论模型将不会有产品关联。
Can you pastebin the Review model, Product model, AppModel, AppController, and the controller you are getting the error from.
The line:
Notice (8): Undefined property: AppModel::$Product [APP/controllers/reviews_controller.php, line 46]
Seems to indicate the Review Model is loading the AppModel and not the file you want it to. In that case the Review model won't have a Product association.
你可以打印堆栈跟踪来查看,这是我从 抓取的一些代码php.net
不过,看完这一切会很困难。
you can print the stacktrace out to see, here's some code I snatch from php.net
It's gonna be hard looking through all that though.