简体   繁体   English

Cakephp 解析错误:语法错误,意外的“类”(T_CLASS),需要标识符(T_STRING)

[英]Cakephp Parse error: syntax error, unexpected 'Class' (T_CLASS), expecting identifier (T_STRING)

I have a table named 'classes' in my database.我的数据库中有一个名为“classes”的表。

When I do a cake bake all classes I'm getting this error:当我做cake bake all classes我收到此错误:

Parse error: syntax error, unexpected 'Class' (T_CLASS), expecting identifier (T_STRING) in C:\xampp\htdocs\timetable\src\Model\Table\ClassesTable.php on line 4

I understand where the error is coming from, but is there a way to get rid of this error without having to rename my 'Classes' table?我知道错误来自哪里,但是有没有办法摆脱这个错误而不必重命名我的“类”表?

I understand where the error is coming from, but is there a way to get rid of this error without having to rename my 'Classes' table?我知道错误来自哪里,但是有没有办法摆脱这个错误而不必重命名我的“类”表?

No you don't.不,你没有。 :) The issue happens because CakePHP tries to bake \\App\\Model\\Entity\\Class.php which is obviously not going to work. :) 问题发生是因为 CakePHP 尝试烘焙\\App\\Model\\Entity\\Class.php显然是行不通的。 The issue is not the table class name.问题在于类名称。

You will have to name the entity class somehow different and tell your table object to work with that entity class .您将不得不以某种不同的方式命名实体类,并告诉您的表对象使用该实体类 This happens because Cake expects the entity to be singular by convention and "class" is a reserved key word in php .发生这种情况是因为 Cake 按照惯例希望实体是单数,而“class”是 php 中的保留关键字 Cake inflects "Classes" to singular for the entity and thats causing the issue. Cake 将实体的“类”转换为单数,这就是导致问题的原因。 So bake your model, change the entity class, then bake the controller and views.所以烘焙你的模型,改变实体类,然后烘焙控制器和视图。

I recommend you to read and remember the CakePHP conventions .我建议您阅读并记住CakePHP 约定

Class is a reserved keyword of PHP , and will cause trouble if you instantiate it as such. Class 是PHP 的一个保留关键字,如果你这样实例化它会引起麻烦。

A solution would be to manually create a Model as thus:一种解决方案是手动创建模型,如下所示:

class MyClass extends AppModel {
    public $useTable = 'classes';
}

You need to ensure that your controller $uses MyClass (inside your ClassesController).您需要确保您的控制器 $ 使用 MyClass(在您的 ClassesController 中)。

After that, the Model should be usable as any other, despite the reserved term.在那之后,尽管保留了术语,模型应该可以像任何其他模型一样使用。 Having said that, I am not 100% on the results of a bake once you've manually created the Model, so please, if you could post your results, that would be great.话虽如此,一旦您手动创建了模型,我就不会 100% 相信烘焙的结果,所以如果您能发布您的结果,那就太好了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel解析错误:语法错误,意外T_CLASS,期待T_STRING - Laravel parse error: syntax error, unexpected T_CLASS, expecting T_STRING Laravel 解析错误:语法错误,意外的 T_CLASS,在 artisan 中期待 T_STRING - Laravel parse error: syntax error, unexpected T_CLASS, expecting T_STRING in artisan PhpSpreadSheets-意外的“类”(T_CLASS),需要标识符(T_STRING) - PhpSpreadSheets - Unexpected 'class' (T_CLASS), expecting identifier (T_STRING) 错误:(!)分析错误:语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE),需要标识符(T_STRING) - Error : ( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) 解析错误:语法错误,意外的“;”,需要标识符(T_STRING)或变量(T_VARIABLE) - Parse error: syntax error, unexpected ';', expecting identifier (T_STRING) or variable (T_VARIABLE) Wordpress-解析错误:语法错误,意外的“函数”(T_FUNCTION),期望的标识符(T_STRING) - Wordpress - Parse error: syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) PHP Parse错误:语法错误,意外T_CLASS - PHP Parse error: syntax error, unexpected T_CLASS 解析错误:语法错误,意外的“ href”(T_STRING),期望为“,”或“;” - Parse error: syntax error, unexpected 'href' (T_STRING), expecting ',' or ';' cakePHP - 解析错误:语法错误,意外的 T_STRING - cakePHP - Parse error: syntax error, unexpected T_STRING 解析错误:语法错误,意外'require'(T_REQUIRE),期望在第203行的ComposerAPI.php中的标识符(T_STRING) - Parse error: syntax error, unexpected 'require' (T_REQUIRE), expecting identifier (T_STRING) in ComposerAPI.php on line 203
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM