Use of uncharted entities in Symfony2

Asked

Viewed 326 times

5

I read that normally the Entity folder of a Bundle stores the entities that will be persisted (in my case, by Doctrine) in the database.

But, due to the ease of working with validations in the "Annotations" format within an Entity, I opted to create uncharted (nonpersistent) entities. In this way, I can validate an arbitrary data structure using all the conveniences of Symfony2 in the "Annotations format".

Therefore, my question is: is there any contraindication of using uncharted entities (by Doctrine) within my project?

  • I’m not familiar with Symfony 2, but that question seems more related to Doctrine itself, @eminetto if you can answer that...

2 answers

3


Answer

There is no contraindication.

You can do as you think by getting the benefits of the validation component.


Reflections

An "Entity" is actually a common object. Through metadata (by annotations or configuration file) is that Doctrine does its job - without the class needing to implement (Implements) an interface or extend (extends) a hierarchy.

So maybe you want to put these classes in one another folder inside the Bundle, since "Entity" is enshrined by the use as the folder where entities to be persisted reside.

In short: the question revolves around what "entities" means... you speak in

uncharted entities (not persistent)

...I’m okay with that, but who knows if the name "entity" refers to justly to "an object that will be mapped/persisted"? Why not simply enunciate "instances uncharted" or "objects uncharted" or "classes uncharted"? Just don’t call your objects "entities"! :-)


Completion

Finally, in practice you can put your classes in the "Entity" folder and use the word "entities", not map, not use Doctrine or ORM annotations, but use validation annotations and the Symfony 2 validation component without problems. And you can also, as said, put these classes in a folder other than the "Entity".

  • I did some research... funny that the documentation of Symfony and Doctrine comes out using the term "Entity" without defining the same at any time. Hard to find anything "conceptual" about the term. I found here: http://whatis.techtarget.com/definition/entity - the use of the term is very linked to database and storage in it. It also says that the meaning is similar to that of "object" in object-oriented programming...

  • The validation component of Symfony 2 is independent not only of the Doctrine and database, but also of the Forms: http://symfony.com/doc/current/book/validation.html - as a rule it is used in an integrated way with Forms and/or ORM.

1

I don’t see why that could be bad, the only difference is that these entities wouldn’t be managed by Doctrine’s Entitymanager, right? You will use them for other functions within the project. Or you will use these entities in some way with Doctrine?

  • I will not use these entities with Doctrine, nor with anything related to databases. They will be used solely to make validations of form data, Hence my fear of seeming to be gambiarra and not be the recommended way to organize my Symfony project. Do you have any other recommendations on this? At first, I imagined that a Symfony Entity must represent an object to be persisted in the database.

  • Perhaps you could use another data validation method, such as Zend/Validator or the Symfony component itself (http://symfony.com/doc/current/book/validation.html). P.S.: I don’t use Symfony, so I might be talking nonsense ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.