1
votes

Using CakePHP 2.x

I have successfully generated many models, controllers, and views but one of them is just not working. the database table is name 'server_cpu', The model appears to generate fine as I have compared it to other models that can be turned into controllers and views and it is identical. It also does have the useTable = 'server_cpu', but even still when I try to generate the Controller it tells me that the model has to have a table.

After looking closely I believe that it is trying to use the table 'server_cpues', How can i force it to use 'server_cpu' and not 'server_cpues', note that I have tried emptying the /tmp/cache/ folder and that has no effect.

The error when attempting to generate a controller for 'ServerCpus' using cake bake: 'You must have a model for this class to build basic methods. Please try again.'

1
Simply by going in your bootstrap and setting the uninflected / irregular inflections. - mark
Why can't you use the correct table format instead? - Miheretab Alemu
Mark can you give a bit more detail and post that as a solution, I believe it may solve the problem - OzzyGreyman

1 Answers

1
votes

There are two possible solutions:

Firstly: simply changing the name of the table can resolve this problem, but it should be noted that for many this is not a possibly depending on the stage of development, for example if the current database is well established and used by many other systems or application this may not be possible. If you are starting from scratch this will be an easier solution.

Secondly: a slightly more complex solution would be to work with Inflectors to change the behavior of CakePHP. This can be done by modifying the file '/app/Config/bootstrap.php' to add a custom Inflector, for documentation on this refer to this for information on inflectors for CakePHP 2.x. For this particular situation you could use something like

Inflector::rules('plural', array('rules' => array( '/(.*)cpu$/i' => '\1Cpu' ) ));

Note the use of regex to recognize all string containing cpu