When should a model not be mapped to a table?

Hello gang. I’m learning MVC and I understand that most of the time, the model represents a table. But I noticed some models don’t correspond to a table, like LoginForm for example. I’m having a hard time understanding why/when models are created without being mapped to a table. For example, for LoginForm, why not just use the User model to check if the user/password is valid?

You can use models without database relation for forms without database table. ;)

For example:

You want to have a "contact" form…

The "Contact" form receives data by the end user…

but you have no “Contact” database table and you dont want to store every single contact… ;)

So you can create a "ContactForm" model do handle the input of the users. (with validation / verification of captcha and actually sending the email etc)

Maybe reading the Yii guide will help to get this more clear:


You’re referring to Active Record (a model directly mapped to a DB table). These are used to abstract away the need for SQL whenever you want to make changes to a model, instead encapsulating this functionality into methods, etc. The other main type of model is a Form Model, which simply takes data from an input, works with it, and triggers the production of an output.

Active Record should be used for data in tables you’ll be manipulating often, either inserting new records, or pulling out existing ones to modify or delete. Use Form Models where you’re only wanting to take a “one time” data submission from a user that you have no intention of actually keeping, such as logging in (where all you’re doing is checking the data the Form Model has captured against your users table) or a contact form (where you’ll probably be using the mailer functionality to send the captured data to your support email).

As you say, you could simply use the User Active Record to pull out a record that matches a login submission, but you’re running the risk of mixing concerns there, as well as breaking the SRP (single responsibility principle) rule-of-thumb. Part of MVC is the separation of concerns within your application, and so becomes an issue of good practice to separate out the User Active Record model from a Login Form Model where the two have differing responsibilities on similar subsets of data.

Thank you!

There are a few standard uses of models without database tables as mentioned above and some more I’ve added below:


[*]Models that are only for capturing data and not saving e.g. contact form

[*]Models that link to multiple database tables such as registering a "user" and "company" on the same form/model

[*]Forms where you want to restrict the data you are working with, for instance a login form that you don’t want to accidentally expose or break most of the user fields like passwords and security questions. You might have several different “User” models for different uses and many might not need to link to the table directly, although they might use the ActiveRecord model underneath.