CakePHP 未在生产服务器上加载与模型相关的属性

发布于 2024-11-19 15:09:59 字数 2524 浏览 2 评论 0 原文

这是一个奇怪的现象。

我有一个本地服务器,我在其上开发应用程序。我开发的产品评论应用程序可以完美地运行它,并利用 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' => ''
    )       
);
}
?>

This is a weird one.

I have a local server on which I develop apps. A product review app I developed works flawlessly on it, and utilizes Cake's associative modeling ($hasMany, $belongsTo, et. al.).

After pushing this app up to a production server, it fails. Gives me an error message:

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

I've debug()'d $this and it shows, plain as day, that, while the local server is loading the associated models, the production server is not. The databases are mirror duplicates (literally, the production server was imported from the dev db), and I can manually load models, which tells me it's connecting to the DB just fine.

What on Earth is going on?

UPDATE

The sql query from the production server is this:

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

The sql query from the dev server is this:

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

Here's some of the code the errors point to:

$title = $this->Review->Product->find( 'first', array( 'fields' => array( 'Product.name' ), 'conditions' => array( 'Product.id' => $filter ) ) );

UPDATE 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昇り龍 2024-11-26 15:09:59

我遇到了这个问题,对我来说,这是由于其中一个数据库表中缺少字段造成的。我会三次检查以确保两个数据库完全相同(尽管你说它们是......):请随意使用这个已有 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...

梦境 2024-11-26 15:09:59

实际上 - 乍一看,可能值得使用 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.

只等公子 2024-11-26 15:09:59

有没有办法不用通过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!

川水往事 2024-11-26 15:09:59

您可以粘贴评论模型、产品模型、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.

她说她爱他 2024-11-26 15:09:59

你可以打印堆栈跟踪来查看,这是我从 抓取的一些代码php.net

 echo "
Stack
"; $aCallstack=debug_backtrace(); echo "<标题><第>文件<第>行<第>函数"; foreach($aCallstack 为 $aCall) { if (!isset($aCall['file'])) $aCall['file'] = '[PHP 内核]'; if (!isset($aCall['line'])) $aCall['line'] = ''; echo "”; } echo "
args
{$aCall['file']}{$aCall['line']}"。 "{$aCall['function']}"; 调试 (($aCall['arg'])); 回声“
"; 死();

不过,看完这一切会很困难。

you can print the stacktrace out to see, here's some code I snatch from php.net

 echo "<div>Stack<br /><table border='1'>";
$aCallstack=debug_backtrace();
echo "<thead><tr><th>file</th><th>line</th><th>function</th><th>args</th></tr></thead>";
foreach($aCallstack as $aCall)
{
    if (!isset($aCall['file'])) $aCall['file'] = '[PHP Kernel]';
    if (!isset($aCall['line'])) $aCall['line'] = '';
    echo "<tr><td>{$aCall['file']}</td><td>{$aCall['line']}".
        "</td><td>{$aCall['function']}</td><td>";
        debug (($aCall['arg']));
        echo "</td></tr>";
}
echo "</table></div>";
die();

It's gonna be hard looking through all that though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文