2

Is there a way to automatically generate models from a MySQL DB in YII?

1 Answer 1

8

You can use Gii to generate models, views and controllers for CRUD operations from tables.

You can enable Gii by inserting this code in your configuration file (uncomment it from the by yiic generated code).

'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'****',
            'ipFilters'=>false, // Not recommended, but easy.
      ),
),

Then browse to example.com/index.php?r=gii. Login to Gii and from there the process speaks from itself.

When asked for the Model name enter * to create models for all tables in the database.

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

7 Comments

+1 <- it's an half-automatic-model creation: Gii generates one model from one table, but Gii doesn't work in "batch mode". That means: it's not possible to generate models for all the tables at ones. You have to create the models step by step.
I know for a fact that I've auto-generated models before on Yii. Sadly I don't remember how
Yii has (er, "had", it's been depreciated in favor of Gii) a command line tool for this, that could be called by a shell script or something to make it more "automatic". Or it could be modified to be more automatic. yiiframework.com/doc/guide/1.1/en/quickstart.first-app-yiic
@The Bndr. not true. Just enter * for the model name and it will generate models for all tables.
@the bndr schemaname.* will generate all the models. I'll recommended updating your yii framework copy.
|

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.