SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast doddsey_65's Avatar
    Join Date
    Sep 2010
    Location
    North East UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    PHP Code:
    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?
    Test and help out with a new
    100% open source forum package: A Simple Forum
    Check the A Simple forum Github Page
    Visit my Github profile to see what I work on.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    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().
    "First make it work. Then make it better."

  3. #3
    SitePoint Enthusiast doddsey_65's Avatar
    Join Date
    Sep 2010
    Location
    North East UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Test and help out with a new
    100% open source forum package: A Simple Forum
    Check the A Simple forum Github Page
    Visit my Github profile to see what I work on.

  4. #4
    SitePoint Addict
    Join Date
    Aug 2013
    Location
    New Zealand
    Posts
    307
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by doddsey_65 View Post
    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?

  5. #5
    SitePoint Addict bronze trophy Hall of Famer's Avatar
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    371
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)
    I think Symfony is quite different from what most PHP MVC frameworks about, it does not label itself as MVC at all.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2013
    Location
    New Zealand
    Posts
    307
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by doddsey_65 View Post
    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?


    Or is there some way to do this in the Entity?
    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.)

  7. #7
    SitePoint Addict
    Join Date
    Aug 2013
    Location
    New Zealand
    Posts
    307
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    I think Symfony is quite different from what most PHP MVC frameworks about, it does not label itself as MVC at all.
    Oh? Why not? What are you comparing it to?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •