views:

444

answers:

3

Cuold someone explain me the difference between the Doctrine auto generated files .class.php and Table.class.php? For example in the Jobeet tutorial there's JobeetJob.class.php and JobeetJobTable.class.php (http://svn.jobeet.org/doctrine/trunk/lib/model/doctrine/sfJobeetPlugin/)

I don't understand the role of each file and where I have to put methods for the model class.

+2  A: 

XXX.class file holds Doctrine_Record descendant, which is intended to operate on a single record. Save, create, edit etc. XXXTable.class.php holds Doctrine_Table descendant, which is intended to operate on a whole table. Searching for records for example.

FractalizeR
Fractalizer, consequently we can infer create, edit, delete functions will belong to XXX.class and all queries such as - select from where >.... will be part of XXXTable.class.php?
sebastian_h
For queries you can better use DQL. It's more powerful.
FractalizeR
+1  A: 

Your Modelname.class.php file holds the container class, for example Post. This class has all the methods and properties of one row in your table, for example the Post table. If you have columns in the table like id, text, etc., you will be able to access them through the Post class.

However, your PostTable (or XxxxxTable class) is the table reference, meaning that this class should be responsible for querying the table to fetch the data.

Let me give a brief example. Lets say you wish to pull out a single post from the table and then edit that.

First, you would do like $post = Doctrine::getTable('Post')->findOneById(1); This is done from the table class because you'll want to pull out some data from a specific table.

Now you have your post (as in a Post object), because ->findOneById(...) queried the database for you. Then you can edit that, for example with $post->title = "a nice title". Finally, save your post with $post->save();.

Exceptions to this is when you want to fetch related objects, which for example could be replies to your post. This would be done through the object you already pulled out to $post.

I hope I made my point clear - if not, please do not hesitate to ask further questions.

phidah
I'd add to further explain the Table class, that you can extend this class's functionality by writing your own methods in it, eg if you always query an Events table by looking for the upcoming events, you could have Doctrine::getTable("Events")->getUpcomingEvents() as an example.
richsage
Yes, but you can do that with the other class aswell. Say you have a model with "firstname" and "lastname" - you'd then be able to make the method ->getFullName(). Important point though.
phidah
A: 

Ah, now we know! :D cheers for the answers.

Jamie