Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do Model classes go in symfony2

I am new to Symfony2. Coming from Zend background, I do not see any folder for Models. How do Models and Controllers communicate?

What if I do not want to use Doctrine. Even if I use Doctrine, where will the Models live and how can they communicate with controllers?

Symfony website has some good documentation about symfony2, but it is not on par with the documentation I noticed for symfony1.X .The official documentation does not have what namespaces should be added when using different doctrine methods. Thanks for the community in advance for the tips.

like image 670
yesIcan Avatar asked Nov 06 '12 19:11

yesIcan


2 Answers

You can create Model manually.

MODELS:

  • Create new dir inside your bundle (Model)
  • Create MyModel

  • Set namespace (company\mybundle\models)

  • Set Doctrine and use entities (use)
  • In your model put DQL

CONTROLLERS:

  • use company\mybundle\models\mymodel;

    public function getRecentUserAction ($max = 10)  
    {
         $user = new MyModel();
         $list = $user->getRecentUser($max) // DQL
        return $this->render('CompanyBundle:controller:index.html.twig',array('list'=>$list));
    }
    
like image 103
Ivan Avatar answered Sep 18 '22 06:09

Ivan


Symfony2 is really about providing the tools for the Controller part, the View part, but not the Model part. It's up to you to create your model by hand or use any other tool, like an ORM.

-- Fabien Potencier (source)

But how do I communicate with the database without a model?

You can choose your way of doing it. You can create your custom Models and use them, or you can use DataMappers or something else. The Symfony2 Standard Edition includes the Doctrine and Propel ORMs. Doctrine is used by default.

Read more about those ORM and how you can use them inside Symfony2 here: doctrine or propel.

like image 45
Wouter J Avatar answered Sep 20 '22 06:09

Wouter J