Help me understand Symfony better (or general MVC)

Ive worked with MVC for a few years now but I have always created my own framework up until I started using Silex. Silex was very similar to the framework I created and so it was easy for me to learn.

Symfony is another story. I am confused as to where to put things. For example the current version of my forum I have a class for every model (ForumModel). In that class I have different methods for getting forums (findAll). I use findAll() to return all forums to the controller and then pass this data to the view so I can iterate through them.

The findAll() method not only gets data from the DB but it also sorts the forums by parents and children,subchildren etc so I end up with a nice array. So where do I put this sorting in Symfony? Do I use the ForumEntity method findAll() and then sort through them in the controller?

Controller:


public function indexAction ()
{
    $forums = $this->getForums();
}

public function getForums()
{
    // Get from database
    // Format into array

    // return format
}

Or is there some way to do this in the Entity?

For general MVC, I recommend reading From Flat PHP to Symfony2.

As for your ForumModel, the Symfony way is to split this up into a couple different classes: an entity and a repository. The ForumEntity would be just a simple PHP class. It’s job is to contain data and behavior that represent the concept of a forum. Then you would also have a ForumRepository, and this is where you’d put database queries.

More info: http://symfony.com/doc/current/book/doctrine.html

The idea of sorting the forums into parents, children, subchildren, etc. isn’t one that you should have to do manually. An ORM such as doctrine should give you the result as Forum objects with all the relations already established. You should be able to write something along the lines of $forum->getChildren().

Thanks for the info.

Getting the children is a little more complicated as it relies on an algorithm to define the children using a modified preorder tree traversal. So the children arent defined by a “parent id” as such.

Just out of curiousity what are the cited advantages of using modified tree traversal over straight database indexes? Is it an attempt to overcome a perceived deficiency of MySql indexes?

I think Symfony is quite different from what most PHP MVC frameworks about, it does not label itself as MVC at all.

If the Django MVC ( or MTV) is anything to go by it sounds like a public method that should go in the Forum model. Although I am not sure where this saying comes from but I’ve heard it said “Keep your models fat and your controllers thin” so anything that can reasonably done in the model methods should probably be done there.

[However, I wouldn’t pass Request or Response objects into a model method as this leads to problems invoking these same methods outside of a Request/Response cycle. ]

I’ve downloaded some documentation on Doctrine to see how they do it. They seem to define an ordinary class which seems to work “magic” with code annotations. (Django does it with class inheritance to a base model class and metaclass “magic” and no annotations wheras doctrine seems to not to use inheritance at all but annotations instead.)

Oh? Why not? What are you comparing it to?