Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models without database access in symfony2

Tags:

php

symfony

I am currently learning how to use the symfony2 framework.

Going through the cook book, I am starting to have a basic understanding of how everything fits together.

However, I have 2 questions regarding Entities, which I believe are models in the MVC pattern:

  • In the documentation, there seems to be a lot of talk about entities using doctrine as ORM. If I have an entity/model that does not require any ORM, is this still considered an "entity"?

  • In most tutorials I have seen, entities are often all thrown into a folder called "entities" under the application bundle. With many entities in an application, I feel that this can become quite messy and unorganized. How can I group and organize entities?

Cheers :)

like image 452
F21 Avatar asked Oct 12 '11 00:10

F21


2 Answers

regarding your question how to organize your model classes:

You may add Subfolders to "Entity" folder, then just follow that scructure in your namespace definition like:

<?php

namespace Acme\SampleBundle\Entity\Subfolder\EntityClass

regarding your question to work without orm: that's simple, just don't use it. your classes will behave like "normal" classes do..

But you will need some kind of interface to that, like EntityManager in Doctrine2, too.

I would always prefer the use of ORM/ODM..

That case I would just add a simple method to your entity class:

<?php

public function sendByEmail() {

// Do stuff
}

You dont have to persist (save to DB) stuff at all. Note that in symfony1.4 there was a save() method on entities. In Symfony2 stuff is saved through $entityManager->persist($entity);

like image 92
mblaettermann Avatar answered Sep 21 '22 12:09

mblaettermann


Entities are models stored in a relational database. Documents are models stored in a document databases (MongoDB, for example).

If you don't want to bend your model to a particular namespace depending on what storage type you're using, here's what I suggest. Create the Model namespace for your model classes. If you choose to use a relational database, you extend your model class and put it into the Entity namespace, providing mapping information in an external file. If you later decide to move to a document database, you do the same, but use the Document namespace.

For a good example of this idea see FOSUserBundle.

like image 26
Elnur Abdurrakhimov Avatar answered Sep 20 '22 12:09

Elnur Abdurrakhimov