0

I have a table named 'classes' in my database.

When I do a cake bake all classes I'm getting this error:

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?

2
  • 1
    Just don't use that name Commented Jan 31, 2016 at 22:02
  • Next time you ask a question make sure you tag it with the specific CakePHP version you're using. Commented Jan 31, 2016 at 23:06

2 Answers 2

1

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. 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 inflects "Classes" to singular for the entity and thats causing the issue. So bake your model, change the entity class, then bake the controller and views.

I recommend you to read and remember the CakePHP conventions.

Sign up to request clarification or add additional context in comments.

Comments

0

Class is a reserved keyword of PHP, and will cause trouble if you instantiate it as such.

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).

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.

1 Comment

He is using CakePHP 3.0 and the problem is the entity, not the Table object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.