间断命名冲突 [英] Intermitent Naming Conflict

查看:143
本文介绍了间断命名冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我呼吁集体智慧在这一个。我有以下非常奇怪的错误。

I call for collective wisdom on this one. I am having the following extremely weird bug.

我有一个名为File.php的模型。我已经在我的应用程序中的几个地方使用它。

I have a model named File.php. I have used it in several places in my app. But now it doesn't seem to work.

在这种情况下,它可以在我的Template.php中使用:

In this case, it works in my Template.php:

        $this->Behaviors->load('Containable');

    $this->contain(
        array(
            'User',
            'TemplatesUserType' => array(
                    'UserType',
                    'FilesTemplatesUserType'=>array('File')
            )
        )
    );
    $template=$this->find('first',array('conditions'=>array('Template.id'=>$template_id,"Template.user_id"=>$user['User']['id'])));

这也适用于FilesTemplatesUserTypesController:

This also works in FilesTemplatesUserTypesController:

            $this->FilesTemplatesUserType->File->recursive=-1;
            $file=$this->FilesTemplatesUserType->File->findByName($name);

            if(gettype($file)=="array" && count($file)>0){
                $file_id=$file["File"]["id"];
            }
            else{
                $this->FilesTemplatesUserType->File->create();
                $this->FilesTemplatesUserType->File->save(array("File"=>array("name"=>$name)));
                $file_id=$this->FilesTemplatesUserType->File->getInsertID();
            }

            $this->request->data["FilesTemplatesUserType"]["file_id"]=$file_id;
        }

        $this->FilesTemplatesUserType->create();
        $this->FilesTemplatesUserType->save($this->request->data);

两种情况下都工作,很好。但现在,从模型Document.php,它失败抛出一个500内部服务器错误:

Both cases work, perfectly fine. But now, from model Document.php, it fails throwing a 500 internal server error:

$this->Behaviors->load("Containable");
    $this->contain(array(
            "UsersVersion"=>array(
                    "conditions"=>array("UsersVersion.user_id !="=>$user["User"]["id"]),
                    "User"
            ),
            "Draft"=>array(
                    "fields"=>array("Draft.id,Draft.template_id,Draft.name"),
                    "Template"=>array(
                            "fields"=>array("Template.id, Template.name, Template.user_id"),
                            "TemplatesUserType"=>array(
                                    "DraftsUser"=>array(
                                        "User"
                                    ),
                                    "FilesTemplatesUserType"=>array("FilesUsersVersion","File")
                            )
                    )
            )
        )
    );
    $draft=$this->findById($document_id);

但是,直到我删除文件工作。

But it is until I remove the "File" that works.

要调试,我在同一个文件中尝试过:

To debug, I tried this in the same file:

$model=$this->Draft->Template->TemplatesUserType->FilesTemplatesUserType->File;
    $model->recursive=-1;

    $file=$model->findByName("Ident");

它会在 CORE / Utility / File.php中的第88行引发错误表示$ path具有: array(class=>File,alias=>File) a string 和以下错误消息:致命错误:调用未定义的方法File :: findByName()在C:\Inetpub\vhosts\\ \\ *******。com\\ \ httpsdocs \eolas\app\Controller\DocumentsController.php on line 245

It throws an error at line 88 in CORE/Utility/File.php indicating that $path has: array("class"=>"File","alias"=>"File") instead of a string and the following error message: Fatal error: Call to undefined method File::findByName() in C:\Inetpub\vhosts\*******.com\httpsdocs\eolas\app\Controller\DocumentsController.php on line 245

让事情更困惑,这从我的Template.php模型:

To make matters more confusing, this works from my Template.php model:

$model=$this->TemplatesUserType->FilesTemplatesUserType->File;
    $model->recursive=-1;

    $file=$model->findByName("Ident");

看起来错误发生取决于模型文件被调用的递归级别,是当它可能与File.php实用程序类冲突。

It appears that somehow the error occurs depending on the recursive level that the model File is being called and that is when it might get in conflict with File.php Utility class.

我的问题是:为什么它在一些地方工作完美,直到现在是失败?有没有什么东西给CakePHP内部的工作,我碰巧正确地碰巧,现在我失去了它?

My question is: Why is it working perfectly in some places and until now is failing? Is there something to CakePHP inner workings that I am hitting correctly by chance and now I am losing it?

我希望你能引导我在正确的方向必须更改我的模型名称,= P)。

I hope you can steer me in the right direction (and not to have to change my model name, =P).

非常感谢你!

推荐答案

如我在回答您之前的问题不能有一个名为 File 的模型,因为已经有一个名为 File 的类CakePHP的核心代码。您需要为模型使用备用名称。

As stated in my answer to your previous question you cannot have a model named File as there is already a class named File in CakePHP's core code. You need to use an alternative name for your model. Think of File as a reserved word.

你的代码有时工作的原因是你的模型或者先触摸Cake的文件实用程序。在您的代码失败的情况下,文件被用作实用程序类,而不是您的模型,因此不兼容。

The reason why your code is sometimes working comes down to whether your model or Cake's File utility is touched first. In the case where your code is failing File is being used as the utility class rather than your model so isn't compatible.

想想:如果Cake尝试启动类 File / * some params * /) File 类会在你的代码中使用吗?事实上,这是不清楚应该给你的线索,你不应该使用与实用程序类同名的模型。我怀疑您的错误日志包含更多关于您的代码的错误/警告。

Think about it: if Cake tries to initiate an instance of the class File (i.e. new File(/* some params */)) which File class will it use in your code? The fact that this is unclear should give you the clue that you shouldn't be using a model with the same name as the utility class. I suspect your error logs contain more errors/warnings about your code as a result of this.

不幸的是,您不得不更改模型的名称。但是,您可以更改一些重命名的模型类的属性,以便它仍然像 File 模型(这是未经测试,因为我从来没有需要做这,但可能值得一试)。例如,将类(和文件名)重命名为 AppFile ,然后设置 $ name $ alias $ table 属性,以使模型表现为 File : - / p>

Unfortunately you have no choice but to change the name of your model. However, you may be able to change some of the renamed model class' attributes so that it still behaves as the File model (this is untested as I've never needed to do this, but might be worth a try). For example, rename the class (and filename) to AppFile and then set the $name, $alias and $table properties so that the model behaves as File:-

<?php
class AppFile extends AppModel {

    public $name = 'File';

    public $alias = 'File';

    public $table = 'files';

}

这篇关于间断命名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆