9

I understand how to query a table with Yii's active record environment. I can't find documentation for calling a mysql view. Also, I know how to model a table in active record with the gii tool, but is there a way to do this for a database view?

2
  • It should work fine in the same way as with tables. Just use the view name when you generate the model. Commented Apr 25, 2011 at 20:40
  • Well that was a silly oversight. Thank you. That worked. Answer the question and I'll give you credit. Commented Apr 25, 2011 at 20:43

2 Answers 2

7

I am not having the same experience and this comment is for those who are having trouble.

Using a mysql view with some joins and calculations I get the following error..

app\\models\\db\\WarComplete must have a primary key.

I resolved this by adding...

public static function primaryKey()
{
    return ['id'];
}

... to my model override.

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

Comments

6

It should work fine in the same way as with tables. Just use the view name when you generate the model.

Please read Joshua's answer and learn how to add primary Key into the OOP model for views.

3 Comments

Not sure why this answer it is marked as correct and has 7 because the active model needs a primary key to work by design and you won't have it in a db you can try the Joshua tip though
@open-ecommerce.org just because Joshua completed 4 years later the answer, and Yii versions have been updated.
Not working, model need primary key, the solution by Joshua is working

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.