Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do entities always have to be based on a database in Symfony2?

Tags:

symfony

I have a project where DB is hidden behind web-services (many of them). In short way - direct access to DB is impossible.

I use Symfony2 and I am forcing myself to use entity every time when I have object which "store data" (for example: user, car, room) and services (accessible from container)/models when more heavier logic is involved (for example TransactionMaker, RoomBooker, CarDestroyer etc).

Entities, without any ORM descriptions, were chosen over arrays because framework provides extremely easy way to validate, build form and IDE intellisense.

Right now it works okay, but some developers claim that an entity must always reflect table in DB. Is that true?

like image 211
mrok Avatar asked Jan 22 '13 18:01

mrok


People also ask

What is a entity in Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.

What is doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.

What is persist flush?

Persist and Flush​ flush() . em. persist(entity) is used to mark new entities for future persisting. It will make the entity managed by given EntityManager and once flush will be called, it will be written to the database.

What is Symfony Doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

How to extend the abstractcontroller in Symfony?

Make it extend the normal AbstractController: Above, add the @Route () - make sure to auto-complete the one from Symfony Components so that PhpStorm adds the use statement. For the URL, how about /admin/article/new:

What are some examples of entities in a database?

If you aren't going to capture data about something, there's no point in creating an entity in a database. If you're creating a database of your employees, examples of entities you may have include employees and health plan enrollment. Are you a student or a teacher?

What is doctrine in Symfony?

Fortunately, Symfony comes integrated with Doctrine, a library whose sole goal is to give you powerful tools to make this easy. In this chapter, you’ll learn the basic philosophy behind Doctrine and see how easy working with a database can be.

What are the entities of an application user?

This exercise produces an initial list of entities as follows: customers, orders, order details, products, personalizations, packages, payments, cards, messages, staff, delivery options, and addresses. Refining the list, you could argue that customers and staff are application users with different roles and permissions.


1 Answers

Entities only map to a database table when they are configured to do so, from the book it reads:

The class - often called an "entity", meaning a basic class that holds data - is simple and helps fulfill the business requirement of needing products in your application. http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class

It is a good practice to model your data in a more formal way than using simple arrays, and this is why they exist.

Perhaps they are most commonly used to map data to the database, but that's not a requirement. They fulfill the purpose of being data-containers that model the information of your application in a way that it makes sense. (i.e. to model a USER in a USER ENTITY)

If you are not using a database to persist your entities feel free to use them to pass data around, to create forms, to use the validation service, security and so on. It could also be a good idea to create a service to allow access to the information on your web services from your Symfony app so you could have something like:

$user = $this->get('some_persistance_service_you_write')->find($id,'user');
$user->setName('new value');
$err = $this->get('validator')->validate($user);
//....
$this->get('some_persistance_service_you_write')->persist($user);

This is, of course, off topic, but it's an example of how could you use entities with no database access.

like image 70
ButterDog Avatar answered Oct 18 '22 21:10

ButterDog