2

I have table name "classes" in database and when i create model with name "Class" ,it gives syntax error (obviously).Is there any way to remove this error without changing name of table in database ?

3 Answers 3

3

I would stay away from a model named "Class", it is a keyword of php and it may will (as pointed by @AD7six) cause trouble if instantiated like that.

You can do the following:

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

Be sure that the controller ClassesController calls MyClass (with $uses), but besides that, you can use the model like any other model without worrying about reserved keywords.

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

1 Comment

may -> it does (it's a parse error - as also mentioned in the question). Worth mentioning associations can be aliased $hasMany => array('Class' => array('className' => 'MyClass')); +1 for including an example.
1

I can't remember the name I used but I had a similar issue with cakephp and after some looking online it worked out to be so much easier to just change the table name. The naming conventions are quite strict and creating a work-around for this isn't worth it.

Comments

0

You could find a different name for your model and then link that model to your table using useTable

hope that helps

Comments

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.