Reputation: 2124
After upgrading from Cake 1.3 to 2.0 I I'm getting missing table errors for all my models that use database views rather than tables. My models using tables still work fine. Changing these models to use tables fixes the problem, but I need views.
In the past this worked, as this question shows; views worked just like tables. However after moving to Cake 2.0 I get:
Error: Database table myView for model myModel was not found
Here's an example Model class:
class Hauler extends AppModel
{
var $useTable = 'hauler_view';
var $primaryKey = 'id';
var $useDbConfig = 'default';
}
The view hauler_view
exists and worked in the app in 1.3. Copying the same data from the view into a physical table will work if I set the model to read that physical table instead.
I've confirmed I can access database views via Cake's raw SQL functions, so I can access those views, it's just that the Models aren't seeing the "tables".
I'm using SQL Server 2005 with the sqlsrv driver if that matters. I have the SQL Server 2008 Native Client installed on my server which allows this version of cake to access SQL Server.
Is there some way to let Models in CakePHP 2.0 use views rather than tables?
I solved this related problem so I now know my database driver is functioning properly and is not the problem.
Upvotes: 0
Views: 3531
Reputation: 2124
This is an Open Bug in the Cake PHP 2.0 branch, the bug affects SQL Server only and doesn't allow models to use SQL Server views. It's slated to be fixed in Cake release 2.0.6.
Upvotes: 1
Reputation: 11575
There are a couple of solutions to try to remedy this issue.
1- Try using the upgrade shell to correct the problem:
2- Check to make sure the model file name is named correctly: app/Model/Hauler.php
[ note case sensitivity on file and path ]
3- Make sure the AppModel.php file exists in the Model directory. This is probably the issue. Note what the book says:
The app/app_controller.php, app/app_model.php, app/app_helper.php are now located and named as app/Controller/AppController.php, app/Model/AppModel.php and app/Helper/AppHelper.php respectively.
And the top of the AppModel.php should contain the uses clause:
<?php
App::uses('Model', 'Model');
class AppModel extends Model {
}
?>
You can read about the AppModel.php changes in the book as well.
Upvotes: 1
Reputation: 873
Try adding this code before opening the class:
App::uses('AppModel', 'Model');
Your code seems correct
Upvotes: 0