0

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.

3 Answers 3

1

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.

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

6 Comments

I've done all of these steps; AppModel wasn't customized so I didn't have it in app/Model but copying the Cake default one into that dir didn't do anything either. The upgrade script didn't fix everything, ran it again and still getting the issue. Most models do work, just not ones with views
The appmodel/ect moving into the app/ directories is a change for 2.1 not 2.0, but thanks for the link, I went ahead and made those changes for future proofing. views are still broke.
I guess the next thing to verify is the SQL Server connection and how it handles views. I don't have access to that so I won't be able to be any additional help. Sorry. But I am glad to hear the setup is correct and can be ruled out. This may be an issue specific to the way the 2008 client communicates with the 2005 server?
Is there anything in the SQL Server logs that appears when trying to connect with the view?
Nothing in the SQL Server log about it, no error messages given from sqlsrv_errors(), cake's own error messages don't given anything but a stack trace and the missing table message. I'll add the stack trace but I don't see any problems, it's all cake internal stuff.
|
1

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.

Comments

0

Try adding this code before opening the class:

App::uses('AppModel', 'Model');

Your code seems correct

3 Comments

You mean put that in the file for the Model's class or the Controller opening the model? Either way it didn't work (since it extends AppModel it should inherit Model anyway). I really don't know what's different about these view models...
yes, CakePHP 2, is necessary. Interesting still does not work, you could put all the code model?
That's the whole code class (which in this case could all be inferred actually) and I'm not customizing AppModel. I'm working through cdburgess' suggestion now

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.