开发者

Simple example with Symfony2 [closed]

开发者 https://www.devze.com 2023-02-10 06:15 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

I read the Symfony2 documentation but i do not quite understand (how to create model, repository, configure doctrine.orm in config.yml and other simple stuffs which are easy in sf 1). So i search a small example which use Symfony2. An very sim开发者_StackOverflow中文版ple example (like the sandbox but little more advanced) with a page which list the content of a table with doctrine ORM and an edit/new page. I don't find nothing on GitHub! Website documentation with real example will be very helpful! Thank you very much...

I continue my dive into sf2...


Symfony2 Bundles is a valuable source of Symfony2-based applications and 3rd-party bundles.

However, you should keep in mind that lots of project you can find out there is out-dated as Sf2 is still not stable and its API is changed quite often.

Basically, all you have to do is:

  1. Ensure that Doctrines' bundles are enabled in your ApplicationKernel.
  2. Make sure it's configured properly:

    doctrine.dbal:
        driver:   pdo_pgsql
        host:     127.0.0.1
        user:     root
        password: password
        dbname:   my_database
        charset:  utf8
    
    doctrine.orm:
        mappings:
            MyApplicationBundle:  ~
            SomeThirdPartyBundle: ~
    
  3. Create some entities.

  4. Although you could use Doctrine2 repositories I'm not a big fan of them. IMO it's better to create your own managers (they can use original repositories) that will provide a transparent API. You shouldn't identify your model layer as ORM only. You could check out UserBundle by FriendsOfSymfony as their approach is pretty good.

Final usage:

$posts = $this->get('myapp.post_manager')->findRecentlyUsed(new \DateTime('-1 week'));

return $this->render('MyApp:Post:list.html.twig', array(
    'posts' => $posts
));


Symfony DIC and config has changed!

You should now use sth like this in your config.yml:

doctrine:
    dbal:
        driver:   pdo_pgsql
        host:     127.0.0.1
        user:     root
        password: password
        dbname:   my_database
        charset:  utf8

    orm:
        mappings:
            MyApplicationBundle:  ~
            SomeThirdPartyBundle: ~
0

精彩评论

暂无评论...
验证码 换一张
取 消