Halfway house Yii

My employer’s codebase is currently procedural spaghetti. Migration to Yii is planned but we wont be able to do that until the team has grown to the point that we no longer need to spend 90% of programmer resources debugging this code.

While there’s light at the end of the tunnel, we still don’t use the framework when developing new features. I’d like to propose we utilise Yiis ORM and validation code in to these features so that we can produce them quicker. It’d also be good training for the team.

Since the ORM is a core part of Yii, it might be difficult to do this (compared to using Doctrine or Propel) so I’d like to ask for help.

Where would I begin? Dependencies suggest I’ll need to create a new variant of CApplication to set up the full Yii context. What would you do?


Create yiiapp.php with the following code:

// change the following paths if necessary






Then in your scrips you will be able to use Yii like this:

include "yiiapp.php";

$cmd = Yii::app()->db->createCommand("SELECT COUNT(*) FROM '{{users}}");

$count = $cmd->queryScalar();


echo "Registered members: $count";

If I understand this correctly, does this mean we can use JUST the ActiveRecord feature of Yii? What about ActiveForm - will that also work?

Sometimes I get work from clients who just need a front end form and admin interface for their existing site. Instead of re-building the whole site with Yii, all I really need is ActiveRecord and ideally ActiveForm too.

Thanks Alexander. I’ll give that a shot and let you know if I find any issues.

ActiveForm would be a bonus. I’ll see what I can do. I can’t believe I didn’t think about just not calling the run() method! ::)

Anyone able to clarify this please?

You might be able to do so - but i don’t think it’s worth the hassle. IMO CActiveForm has too many dependencies on other components (e.g. Yii::app()->clientScript to publish the js assets). So you’d have to dig deep into the code to get a standalone version.

I see. So can I still use all CActiveRecord functions, such as findByPk(), save(), validate() ?

Yes, that should work. Maybe just give it a try?