我呼吁集体智慧。我有以下非常奇怪的错误。
我有一个名为File.php的模型。我在我的应用程序中的几个地方使用过它。但现在它似乎没有用。
在这种情况下,它适用于我的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->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内部服务器错误:
$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);
但直到我删除有效的“文件”。
要进行调试,我在同一个文件中尝试了这个:
$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")
而不是string
,并显示以下错误消息:Fatal error: Call to undefined method File::findByName() in C:\Inetpub\vhosts\*******.com\httpsdocs\eolas\app\Controller\DocumentsController.php on line 245
为了让事情更加混乱,这可以从我的Template.php模型中找到:
$model=$this->TemplatesUserType->FilesTemplatesUserType->File;
$model->recursive=-1;
$file=$model->findByName("Ident");
看起来错误的发生取决于调用模型文件的递归级别,也就是它可能与File.php Utility类发生冲突。
我的问题是:为什么它在某些地方工作得很好,直到现在才失败?对于CakePHP内部工作有什么东西,我偶然正确地打击,现在我正在失去它吗?
我希望你能引导我朝着正确的方向前进(而不是必须改变我的模型名称,= P)。
非常感谢你!
答案 0 :(得分:1)
正如我对your previous question的回答中所述, 核心代码。您需要为模型使用替代名称。将 您的代码有时工作的原因归结为您的模型或Cake的 考虑一下:如果Cake尝试启动类 不幸的是,您别无选择,只能更改模型的名称。但是,您可以更改一些重命名的模型类'属性使它仍然表现为File
视为保留字。File
实用程序是否首先被触及。在您的代码失败的情况下File
被用作实用程序类而不是您的模型,因此不兼容。File
的实例(即 File
),它将在您的代码中使用File
类?这个不清楚的事实应该给你一个线索,你不应该使用与实用程序类同名的模型。我怀疑您的错误日志包含更多关于您的代码的错误/警告。new File(/* some params */)
模型(这是未经测试的,因为我从未需要这样做,但可能值得一试)。例如,将类(和文件名)重命名为File
,然后设置File
,AppFile
和$name
属性,以使模型的行为为$alias
: - $table