Symfony2 Entity with DBAL (No ORM) - symfony

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.

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.

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

Related

partial use of doctrine on an existing php project

I am a new developer on an existing php project that respect its proper mvc.
I have successfully "plug" a Symfony installation on it, in order to replace some of the already existing Symfony components such as Router, Request etc...
I have some functionalities to develop and I am isolating them under one bundle.
My question is : can I use Doctrine for this one in order to start a sanitize work on the existing database ? If I want to use foreign keys with the existing others tables I need to configure the mapping on them...It's a problem because I cannot start a refactoring for the objects of this project that are not entities-like. Is there a solution to use doctrine only for my bundle and keeping the use of foreign-keys, cascading etc... ?
Thank you for all
If you wish to only generate the entities for your isolated bundle, you can do it.
Check documentation: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_entity.html
Are you planning to create your tables in the same database or in a differente database?

Generate entity with PHPCR

Earlier, I used pure essence created a symfony that app/console doctrine: generate: entites, but at the moment, it is necessary to deal with the Symfony CMF. Could you write commands that you need to do in order to register the entity and generate getters and setters, which can juzat in the controller. For example, I need to create a username field, a password. Thanks for the help
For Doctrine PHPCR-ODM, the generator command has not yet been contributed by anybody. There is a discussion in the github tracker and it would be great if somebody ports that code from doctrine ORM or writes a generic generator in the process.
Luckily, an IDE can also generate getters and setters, and the mapping is not that complicated to write. You find the documentation in the Doctrine documentation.

How does Symfony CMF saves into database?

I get that it saves into PHPCR through Doctrine ODM.
But I get that it is all saved in app.sqlite, but I don't see how does it works conceptually ?
Why does it need the database at all when everything is saved in app.sqlite?
Why do I need to make this kind of commands php app/console doctrine:phpcr:init:dbal and so on ?
Symfony CMF can work with different model classes. we provide a default implementation that is mapped for Doctrine PHPCR-ODM. PHPCR-ODM in turn is built on the php content repository PHPCR. there are 3 implementations of that, the cmf sandbox by default uses jackalope-doctrine-dbal. jackalope-doctrine-dbal in turn uses doctrine dbal to store content into databases supported by doctrine dbal. jackalope currently handles sqlite, mysql and postgres. jackalope-doctrine-dbal needs some initialization on the first run, which are the commands you have seen. this is the same whether we use the embedded sqlite database driver or mysql or postgres.
You find some background for the choices in choosing a storage layer and information how to set up phpcr-odm in Create a New Project with PHPCR-ODM. Look around at http://symfony.com/doc/master/cmf/index.html to have most of the CMF concepts and implementation explained.

Doctrine2 does not generate getter/setter

I'm trying to reverse engineer a db schema using Doctrine.
If i do
php app/console doctrine:generate:entities NS/MyBundle/Entity/MyClassName
it successfully creates a class with private properties taken from DB, but there are not getter/setter method, so that i cannot use it. What am i doing wrong?
Please follow How to generate entities from existing database? As I don't think what you are doing is really going to work.

Namespace alias for Doctrine

I became responsible for a large legacy web application and I am trying to slowly refactor it into the Symfony2 framework. The first thing I have done is to include Doctrine.
I have installed Doctrine with the help of Composer and set up a bootstrap file for it. My entities, to avoid future complications, already follow the namespacing scheme Company\BundleName\Entity\Object. The following works:
$em->getRepository('Company\\BundleName\\Entity\\Object')
->find($id)
;
I was unable to find any reference of how to inform Doctrine of namespace aliases as Symfony2 does, so I can write
$em->getRepository('CompanyBundleName:Object')
->find($id)
;
instead. How can I achieve that?
There is an easier way now:
$config = Setup::createAnnotationMetadataConfiguration(...);
$config->addEntityNamespace('CompanyBundleName', 'Company\BundleName\Entity');
will do what you want. It took me several hours hunting to find this! Its not in the docs anywhere I could find.
The functionality for this is set up in Symfony2 by the DoctrineBridge bundle, specifically the getMappingDriverBundleConfigDefaults function.
If you want to reflect this functionality without Symfony2, you'll need to extend the Doctrine entity manager and generate the prefix yourself in the getRepository function. It is not part of the Doctrine system.

Resources