0

I'm working on a SF2 project where I can't use Doctrine2 as ORM, meaning that I already have a database with tables and data. I have to use plain SQL in my controller (I'm currently using DBAL to do that), and I have to create object in order to represent things.

When I used to work with Doctrine2, I create Entity by app/console doctrine:generate:entity and Doctrine2 is handling the whole stuff (update, persisting...)

But now, as I'm using DBAL, how can I create object (can I call it entity even if i'm not using ORM?) to fit my need ?

I was planning to do like usual : create an entity folder in my bundle with entities as objects without the ORM annotations, and create a method where I retrieve data from database using SQL (result of the query in an array) and hydrating it using getters/setters from the object.

Is it a good idea or do you have a better solution ? I'm beginning with SF2 and I read that some people create a service to retrieve data and then using data transformer to transform data into the object.

Thank you.

YukiAsuna
  • 92
  • 9

2 Answers2

2

You are describing Active Record pattern. For this purpose you can use Propel that has native integration with Symfony. Read about it on Symfony's official documentation.

Also I would recommend you to use ORM. You can set your mapping with existing tables as you want: you can even omit some fields if you don't need them in entity. And Doctrine ORM will do all the hard work for you.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
0

You can still use ORM without changing your database by creating classes from your database. You should read this in the symfony documentation

Fabrice Kabongo
  • 671
  • 10
  • 23