Symfony difference between <ModelName>.class.php and <ModelName>Table.class.php
Cuold s开发者_StackOverflow中文版omeone explain me the difference between the Doctrine auto generated files <ModelName>.class.php
and <ModelName>Table.class.php
?
For example in the Jobeet tutorial there's JobeetJob.class.php and JobeetJobTable.class.php.
I don't understand the role of each file and where I have to put methods for the model class.
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.
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.
精彩评论