Using SQL directly instead of Yii ActiveRecord/ORM

Hello, this is my first web-development experience and after reading about various offerings in PHP framework space I decided to go with Yii framework even though it's quite new in the market.

I don't see Yii samples to use controller/model/view without the ActiveRecord.

I don't feel comfortable taking dependency on ActiveRecord/ORM, learn new synctax to define relations, performance issues that might surface because of the extra abstraction down the road but would prefer to use SQL directly in models which I have more control on.

Though I can try to figure out how to use model directly with SQL and plug-in the models directly with controller & model… I wonder if anyone has already done this and can share the details.

It would be nice to have Yii providing such samples on the site.




This page describes how to use SQL directly without touching AR.

Because you don't want to use AR, in order to handle form inputs, you may consider CFormModel.


Thanks qiang for quick response.

I'm confused why you need to declare each member variable in LoginForm class and do the massive assignment as shown below in code-snippet copied from the link you sent. Why not use directly the _POST attributes for validation instead of first copying them locally and validating them. Am I missing something?

$model=new LoginForm;


    $model->setAttributes($_POST['LoginForm'], 'login');

Thanks again.

The reason is that not every data in $_POST is needed.

By calling setAttributes(), we filter out unneeded data, and the populated form model may be used safely for various purposes.