Difference between FormModel and AR

Hi everyone,

I am a bit confused about using FormModel and AR.

The definition for both of them are very clear when I read them.

When I am ready to get my hands dirty,

I realize that I need to use both model when I am dealing with a single database table.

For example, I have a table call ‘user’ and I have created an AR model of it.

I have also used Yii shell to create the CRUD controller and view of the table.

Creation of user is straightforward, fill in the form, hit enter,

AR model populated and it is saved into the table.

The confusion comes when I want to implement a login view.

I have to create a FormModel because FormModel are meant for collecting user’s input.

Why can’t I use the AR Model for collecting user’s input?

With that in mind, I set out to experiment.

I can use the AR model to collect data from the browser.

I decided to experiment with FormModel.

I can use FormModel to contain the data from the database and send it to the view.

But the codes are more complicated than using AR model.

I prefer using a single model class for interaction with browser and backend.

I think in CakePHP, it uses a single model class.

The last thing I want to do is created a lot of model classes.

Anyone can advise on this.

Hi pioneer and welcome to the forums!

I’ve been facing this problem recently and found out that using models is absolutely not forced by the framework itself.

CFormModel is really only a sophisticated wrapper for input you’d like to sanitize at one single point.

Talking about login mechanism, I did not use any form model, not even user identity classes. I grabbed both nickname and password from post data, passed onto an extended CWebUser and confirmed their validity there.

All classes, methods, strings and bytes are there in the framework to use and reuse. But, I suggest you not to take anything without prior decisions. There are cases when you ought not to follow some unwritten principles just for the feeling of perfection.

For the login case, it is fine using either an AR model or a form model. The good thing about using AR model is that you avoid writing another model. The bad thing is that you may introduce some additional attributes (such as ‘rememberMe’) that are only used by login. Also, if you use form model, it is easier for you to reuse it in future since it is not tied with a user AR which may be different in different projects.

Thanks qiang and pestaa for your prompt reply.

Can I use FormModel for accepting inputs from the view as well as returning results to the view?

There may be a situation where I need to return results gather from a few tables

I don’t think AR model can do that because each AR model is specific to a single table.

There may be a situation where I do not want to return all the fields in AR model to the view.

I might return just one or two fields of the AR model. I think FormModel is more appropriate for that right?

For both of your situations, AR model is fine as long as there are some database tables backing your form.

For the first situation, you may refer to: http://www.yiiframework.com/doc/cookbook/19/

For the second situation, you may use scenarios (see the guide).