tags:

views:

99

answers:

2

Hello

I`ve been wondering this one thing about creating models. If I make for example Page model. Is it the both: It can retrieve one row from the table or all the rows. Somehow Im mixing the objects and the database.

I have thought it like this: I would have to make a Page-class that would represent one row in the table. It also would have all the basic CRUD-methods.

Then I would have to do a Pages-class (somekind of collection) that would retrieve rows from the table and instantiate a Page object from each row. Is this kind of weird?

If someone could explain to me the idea of model throughout.. Im again confused.

Maybe Im thinking the whole OOP too difficult..

And by the way this forum is great. Hopefully people will just understand my problems. Heh. I was a long time procedural style programmer and now in 3 months I have dived into OOP and MVC and PHP frameworks and I just get more excited day by day when I explore this stuff!

+1  A: 

Is it the both: It can retrieve one row from the table or all the rows.

You can use a model to interact with the database, there by you can do anything you like, for example getting one or more records, inserting records, updating, etc.

The way to go I would suggest you is to create a model for each of the distinct pages of your site that might interact with the database. You might want to create different functions inside a single model for a single page based on the page's requirements to interact with the database.

Sarfraz
+1  A: 

That depends on whether your Models represent instances or are just ORM objects.

If a Model represents an instance, then each record (row) in the database would become a new Model object. "All records" would simply be represented by an array of these objects. Ruby on Rails for example does it this way.

If your Model is rather an ORM object, it just represents the database as such and allows you to retrieve records from the database through it. The results may be in some container object or just a normal array. CakePHP for instance uses this method.

deceze
Cake and RoR use ActiveRecord. There is no reason why a Rowset ('all records') has to be an array. Can as much be an SplObjectStorage or a dedicated rowset class, like Zend_Db_Rowset. I'm not sure what you mean by ORM object, but I guess you mean a QueryObject. Be aware that there is also TableData Gateway, RowData Gateway and DataMapper. And the Model isn't just the database anyway.
Gordon
@Gordon The "ActiveRecord" used by Cake is not really an ActiveRecord. You can't have multiple instances of a Model, there's only ever one instance into which you feed data or which you get data out of. Call it a QueryObject or whatever else you like. Please forgive the rest of this simplified answer for not covering all possible cases, I tried to illustrate the general difference for the OP.
deceze
I'm pretty sure I have written Cake code that does `new User` wherever I pleased. Ah well, but it's been some time since then. Maybe they changed it :)
Gordon
@Gordon Cake's models are really quite hacky. They tried to copy RoRs way of handling data without going fully OOP for PHP4 compatibility. The result is an "ActiveRecord" that's really a "QueryObject" which can be made to work like one-record-one-object, but is really not meant to. `$Model->find('all')` always returns a simple array of data, while `$Model->read()` also returns a data array but also sets the data internally. Data arrays are clearly preferred to RoR style models.
deceze
`str_replace("Cake's models are", 'Cake is', $previousComment);` ;))
Gordon