SitePoint Sponsor |
|
User Tag List
Results 1 to 24 of 24
-
Jan 4, 2007, 09:36 #1
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A question about the model (in MVC)
Hi,
In MVC (although I guess this doesn’t just apply to MVC) should my model classes/objects represent an entire database table, or an individual record in that table?
For example, I have a table called “user”, and at the moment I have a class called “UserModel”, which extends the “Model” class. I guess it’s possible to manipulate the entire table and the individual records in one class, but something like this just doesn’t seem to make much sense in my mind:
PHP Code:<?php
$user = new UserModel();
$user->username = 'test';
$user->password = 'password';
$user->save(); // this would save those values as a new record
$jack = $user->findAll("username = 'jack'");
?>
Any comments would be greatly appreciated.
Thanks,
Jack
-
Jan 4, 2007, 10:38 #2
Model represents a real business object. It's not just a data source. With that said, it can implement any of your said patterns and more.
Saul
-
Jan 4, 2007, 10:48 #3
- Join Date
- Sep 2005
- Posts
- 55
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In MVC (although I guess this doesn’t just apply to MVC) should my model classes/objects represent an entire database table, or an individual record in that table?
There is also the fact that the model does not have to be a database table does it? So in another model the whole idea of tables and records may not apply.
-
Jan 4, 2007, 10:52 #4
-
Jan 4, 2007, 10:56 #5
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah, I have been looking at active record, and that's where I got the idea of how to do it, what I don't really understand is why if an active record object represents a single row of a single table, why would you have a findAll method as part of that object? Surely it should be in another object, one that represents the whole table, rather than just one record?
** This is assuming the findAll method returns some sort of array of active record objects or something.
-
Jan 4, 2007, 11:03 #6
- Join Date
- Sep 2005
- Posts
- 55
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
http://www.martinfowler.com/eaaCatal...taGateway.html
may help. then there's data mapper if you're feeling brave and dont mind doing some more complex work.
-
Jan 4, 2007, 11:03 #7
-
Jan 4, 2007, 11:34 #8
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm, well i've been reading through some rails tutorials, and from what I saw i thought findAll was use to do a query on the table with some conditions, the same as findByName is used to do a query but using one condition. I assumed it was part of the model object, but I guess not. Anyway, forget all that. This is what im going to do:
My UserModel object will extend a class called Table, which will represent a database table, and have functions for querying that table for records (find, findByName etc.). The records that come back will be some sort of active record objects, and with those you can do things like modify one of the fields and then save it back to the database.
What do you think?
-
Jan 4, 2007, 11:47 #9
- Join Date
- Dec 2004
- Location
- virginia
- Posts
- 188
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I haven't put much thought into this but what about something like this:
PHP Code:$jack = $user->username->findAll("jack");
PHP Code:Class UserModel
{
public $username = new UserNameModel();
}
PHP Code:$jack = $user->UserName()->FindAll("jack");
PHP Code:Class UserModel
{
private $username = new UserNameModel();
public UserName()
{
return $username;
}
}
-
Jan 4, 2007, 11:55 #10
So you decided to use Table Gateway pattern (the link smithie posted). Yes, it is one way to do it. Keep in mind though that, as I said, Model is not the same as Data Source, and as mentioned previously, the source can be not only database. Thus your base Model class should not rely on one or another Data Source Architectural pattern.
However, I think it is a common practice to do so, perhaps because of the nature of most web applications. So that's just my humble opinion.Saul
-
Jan 4, 2007, 12:05 #11
-
Jan 4, 2007, 12:25 #12
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There is never an easy, clear cut answer to this question I find, but for what it's worth I just have two generic class methods, *Record::get(); and *Record::getAll();
What data is returned is upto how you build the SQL in regards to JOINs and WHERE clauses, etc on that Record, ie
PHP Code:public function authenticate( $username, $password, $is_hashed = false ) {
$username = $this -> conn -> escape( $username );
$password = $this -> conn -> escape( $password );
$this -> where( "users.email = '".$username."'" );
$this -> where( "users.status = 'enabled'" );
try {
$this -> get();
...
-
Jan 4, 2007, 12:25 #13
- Join Date
- Dec 2004
- Location
- virginia
- Posts
- 188
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jan 4, 2007, 12:35 #14
-
Jan 4, 2007, 15:08 #15
- Join Date
- Sep 2005
- Posts
- 55
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
id second that. definitely overkill and unwise to have an object for each column. plus, think of the maintenance headache of such a system.
most of the time, the underlying data storage is some kind of DB, and so active record (probably used by most; rails uses it also), table data gateway, and data mapper are available to you.
FWIW (IMHO of course) active record/table data gateway will be more than sufficient for the average web app.
-
Jan 4, 2007, 15:16 #16
- Join Date
- Dec 2004
- Location
- virginia
- Posts
- 188
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
something like:
PHP Code:$user->Select("username");
$jack = $user->findAll();
Sorry if I didn't understand your statement. I'm going back and forth with PHP and C#...and I sometimes mix the two. Properties in C# are formalized getters/setters..so I'm assuming this is what you meant by property.
-
Jan 4, 2007, 15:33 #17
Not exactly. You can call them variables with no harm I guess, just that in OOP class variables are called properties. Yes, I know there's a distinction between the two in C#, but that's just something C# does for you. In PHP you still need the setters/getters only they are not formalized as in C#. And thus you have to call them manually instead of just assigning the value.
What I mean:
PHP Code:$user->setUsername("username");
Saul
-
Jan 4, 2007, 17:07 #18
- Join Date
- Dec 2004
- Location
- virginia
- Posts
- 188
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm assuming that the user class would inherit from a DB class or own a DB object...since the SQL for setUsername(), setPassword(), setLocation(), setEmail() would be mostly the same.
That seems to be about the best way to do it. Thanks for the insight.
-
Jan 4, 2007, 18:04 #19
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, I think so
seems like a good way to go, that way the methods to access lists of records and the methods to modify data of specific records would be in separate objects.
Good question...something about the 'username = jack' as a argument in the findall method bothered me.
PHP Code:$jacks = $user->find("name = 'jack'");
Thanks for everyone's help, if you think of anything else I would be happy to hear it.
-
Jan 5, 2007, 06:13 #20
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jan 5, 2007, 06:17 #21
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah, well, just a different way to do the same thing, I actually have two methods now, one like this:
PHP Code:$jack = $user->find("name = 'jack'")
PHP Code:$jack = $user->findBy('name', 'jack')
-
Jan 5, 2007, 06:36 #22
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The problem with your find() function is that you have to manually escape strings.
If you want support for more complex queries, you could introduce a Query Object:
PHP Code:$jacks = $user->find(new Criterion("name", "jack", CRITERION_EQUAL));
-
Jan 5, 2007, 07:18 #23
- Join Date
- Jul 2006
- Posts
- 80
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah i see what you mean, i will probably look into a query object at some point, but at the moment I'm just trying to get the basics of the table and record objects in, so i can at least start running basic queries and stuff.
-
Jan 5, 2007, 18:10 #24
- Join Date
- Nov 2006
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
or just use parameterized queries, e.g. PDO can do parameterized queries
PHP Code:$jack = $userFinder->find("username = ?", "jack");
Bookmarks