Models without database access in symfony2

I am currently learning how to use symfony2 .

Looking through the cookbook , I begin to understand how everything fits together.

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

  • The documentation has a lot of talk about objects using a doctrine like ORM. If I have an entity / model that does not require ORM, is this still considered an โ€œentityโ€?

  • In most of the tutorials I've seen, objects often end up in a folder called "entities" in the application bundle. With many objects in the application, I feel that it can become quite messy and disorganized. How can I group and organize objects?

Greetings :)

+6
source share
3 answers

regarding your question how to organize your model classes:

You can add subfolders to the Entity folder, and then simply follow this structure in the namespace definition, for example:

<?php namespace Acme\SampleBundle\Entity\Subfolder\EntityClass 

regarding your question, work without a form: simple, just do not use it. your classes will behave like "normal" classes.

But for this you will need some kind of interface, for example EntityManager in Doctrine2.

I always prefer to use ORM / ODM ..

In this case, I just add a simple method to the entity class:

 <?php public function sendByEmail() { // Do stuff } 

You do not need to save permanently (save to DB). Note that in symfony1.4 there was a save() method for entities. In Symfony2, material is saved through $entityManager->persist($entity);

+3
source

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

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

For a good example of this idea, see FOSUserBundle .

+4
source

When you report that you have an entity / model that does not require ORM, you mean:

  • The model is very simple, so you can create your own database queries, OR:
  • Data can be stored in regular files.

?

If this is the first case, you can manage it however you want, but it is good practice to use ORM.

If this is the second case (as I assume), you are still better off making Entities classes. Itโ€™s just that you reference your data differently inside your classes. Instead of querying the database you are looking for in files.

By doing so, you preserve the best practices, and if at some point you want to switch to the database model, you will have to change these classes.

To answer the second question, I have a model with 50+ entities, and this is not scary. Inside the Entities classes, I tell the user where to find the entity-relationship model, so this is understandable.

Does this help you?

0
source

Source: https://habr.com/ru/post/899086/


All Articles