Using Yii generated CRUD models for a plain PHP application.


I have a plain PHP webapplication & I have installed Yii into a SEPARATE SUB-directory.

Now I have Gii/GRUD generated models inside my Yii SubDirectory.

I would like to use this models in my plain PHP webapplication.

For Ex


Contains plain php web application


Contains yii application

A sample model, (Gii generated CRUD)


i want to use this Products models class in my plain PHP pages.

like in


I want to add

$prodObject = getYiiModelClass(‘Products’);

And use existing CRUD functions like $prodObject->save();

How can I reuse this Yii models outside yii application ?

Well… If I’m not mistaken - you can’t.

Method save() is a part of ActiveRecord, which uses so many other classes (e.g. whole database layer) that using it inside plain non-yii application would be like rewriting (reintroducting) half framework code, if not more.

This leads us to a question, why not rewrite your plain application in Yii?

The other, also very important question is if you are legally eligible to do so? I’m not sure if Yii licensee allows you to use only parts of framework in your own code. I would be pretty surprised, if it would allow to do so.


I can actually change it to a complete yii application / load complete yii libs

before loading the model. (if necessary)

$prodObject = getYiiModelObj(‘Products’);

The getYiiModelObj function can contain full bootstraping yii web application code.

For example

function getYiiModelObj($modelName){



defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,3);


// Just initialising instead of RUN()ing could be better




    return new $modelName;[/indent]


return null;[/indent]


But this is not working. Some error from YiiBase when getDB().

My yii part of the application is already using the model & I want the model code to be available in all part. I dont mind loading/initialising a complete yii web application only to load a model.

Correct me if Im wrong.

I have a working plain php web app which I would like to convert into yii app.

But im the only coder so i should do this step by step.

So Im planning to start from model layer. Is this a wrong way ?

To be honest, I can’t help you more. Even at this point I’m pretty sure that you will spend much more time on attempts to use parts of Yii in your plain applicaiton than you would spend on rewriting it completely into Yii.

It depends on how your application is written. If it utilises MVC (model, view controller) design pattern then switching to Yii shouldn’t be much a problem. If it isn’t - there might be a much more work here.

And yes - I would suggest starting from creating models.

My application is completly NON-MVC php application without any frameworks & its too huge.

I need to include atleast the models at first.

But still… calculate this correctly and even gave your calculations to someone else to check, if you’re unsure. Because I’m still more than sure that overall, total work you spend on adapting your application (no matter how huge it is) to be able to use parts of Yii (if it is possible and legal) will cost you more time than starting whole work from scratch - if you can only afford this.

Take into consideration that problems (like missing classes, errors) that will come out during this work, which probably wouldn’t come out if you rewrite it from scratch. Plus that writing application from scratch lets you rethink many solutions and see many problems (or possible problems) that you wouldn’t found else way.

And I’m still really concert if this is legal (have you checked licensee to see, if you can use only parts of framework?) and if this is possible. For example: if you want to use just models, you have to use CModel and CActiveRecord classes, which leads you to requirement of using whole DB layer, which then leads you to use parts, if not whole application layer (and some or all of it’s classes). Plus interfaces being introduced at some level and used at other one. Etc., etc.

Before you decide to pushing toward using only part of Yii instead of rewriting whole application, please take a careful look into Class Reference to see relation between classes - what descends from what, introduces and requires what etc.