Cyrcle
Cyrcle

Reputation: 1373

What is an Entity in Symfony? ~/src/Acme/TaskBundle/Entity

I'm going through "The Book" and it has me create a file in ~/src/Acme/TaskBundle/Entity. But I can't figure out WHY it goes there. What are entities?

Upvotes: 1

Views: 2132

Answers (3)

Marco
Marco

Reputation: 226

The entity object is an abstraction for you to interact with database. For example, instead of doing

UPDATE Book
SET title="My Book"
WHERE isbn="123456789";

You can simply access the object

/** @var $book \Acme\TaskBundle\Entity\Book **/
$book = $repository->findOneByIsbn('123456789');
$book->setTitle('My Book');

/** @var $em \Doctrine\ORM\EntityManager **/
$em->persist($book)
$em->flush();

There is a lot running behind. For more information, please consult Doctrine ORM documentation.

The Acme\TaskBundle\Entity path is the default location for Doctrine to load Entity definition.

Upvotes: 1

Noy
Noy

Reputation: 1268

An entity is an object that represent the underlying data (as @perovic said: exactly one line of data from a single table, joined with data from other tables).

From Wikipedia's "Entity" definition:

A DBMS entity is either a thing in the modeled world or a drawing element in an ERD.

(The full concept of computer engineering entity is defined in Wikipedia's "Entity–relationship model" definition)

In Symfony's Documentation "The Book", chapter "Databases and Doctrine", the "product" object is the entity. It's relationship with the RDB is described under the title "Add Mapping Information".

In Symfony, the entire model (the data tier) is persisted (saved, updated) and managed through Doctrine.

This is just the main concept. More information can be found here:

(WOW. writing all that with 2 links and no images was TOUGH :P )

Upvotes: 1

Jovan Perovic
Jovan Perovic

Reputation: 20201

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. Symfony2 enforces entity syntax Bundle:EntityName, so when you say fetch me all the data from "AcmeTaskBundle:SomeEntity" it knows to look for class file at Acme\TaskBundle\Entity\SomeEntity.php

You should probably watch some tutorials on ORM first (Symfony2 uses Doctrine ORM by default), or skip data persisting altogether for now...

Upvotes: 1

Related Questions