partial use of doctrine on an existing php project - symfony

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?

Related

How to apply new changes to current database in code fist EF core?

I have a project that coded with DotNet Core code first.
Now, there is a lot of real data on the database and i have to add some features with many tables and new relations to this project.
How can I apply these tables and their relations to the Database without adding manually?
Is there any way to use Add-Migratiosn and Update-Database to apply new changes via EF core instead of manually applying?
If you have not many changes to your DBContext you can remove it and use Scaffolding to create new DbContext from the existing database schema. You can find more information in the official Microsoft documentation here.
It is better to use only one entity framework database approach either Database-First or Code-First.

MVC via Symfony

There is a project in Symfony, who want to remake using MVC. The project has controllers and twig patterns , it is only necessary to fasten model.In which Symfony project directory to properly create model files ? Is there anything in Symfony tool allows to do it , rather than just to create these files and to connect them to the controller ?
Use Symfony standard edition as your starting point. If you try to make something own without prior experience with code organization in Symfony you will make it only worse.
https://github.com/symfony/symfony-standard
Standard edition above 2.8 version is pretty slim and doesn't force you to use everything if you don't need to.
If you need only "Model" classes then create them in src/AppBundle/Model and if you need Entities that will be persisted in database put them in src/AppBundle/Entity. Read about generation here http://symfony.com/doc/current/doctrine.html I will help you started

Symfony2: Creating entity table conditionally

I have a bundle with entity defined in it. I want to be able to configure this bundle in such a way, that this entity will or won't be relevant. So if bundle is configured properly entity table shouldn't be created with app/console doctrine:schema:update etc, or should be - it should depend on configuration.
How to conditionally "disable" entity so its table won't be created by app/console doctrine:schema:update?
Your scenario requires you to disable the auto_mapping, but it seems to be set to false by default. http://symfony.com/doc/current/reference/configuration/doctrine.html
Next thing to do is make sure the build function of your bundle conditionally adds the wanted DoctrineOrmMappingPass as also is explained here: https://stackoverflow.com/a/26975083/1794894
As you can see in the source, build only is executed once the cache is empty so this is the place where you can do this. You can also take a look at how to add compiler passes there.
I think that although maybe you could find a way, you are complicating your self. If the back-end bundle is independent then always could be optional to install it and by consequence it's entities created or not.
You can find an example in Sonata bundles, you can manage the users as you want, but if you are using FOSUserBundle, the you have the option to install SonataUserBundle, then tell to fos_user configuration that the new class belong to the Sonata User and as consequence the new entity will be persisted with a lot of new attributes thanks to class inheritance, and all the crud operations for user will be already configured in sonata views. SonataUser also have it's own user entity for using in a standalone way.
I know that this is not what you asking for but may be you just need manage to follow a model like this.

Symfony2 Entity with DBAL (No ORM)

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

Basic inserts (for the application to work) in symfony2

Is there a way in symfony2 that you can pre-define a procedure for inserting the basic data to the database for the application to work?, this is for not sharing an SQL script.
I see this as a doctrine procedure that is executed when you decide to update the schema. So it will check if there is the basic configuration data, if not, it will insert it. Is there something like that?
Thanks in advance
From the Symfony2 website
You have to use DoctrineFixturesBundle
There isn't such a feature embedded in SF2 and / or Doctrine2.
But you can create your own command to add this behavior to your application / bundle :)
As Picoss said, there is the Doctrine Fixtures Bundle but it isn't part of the Symfony2 core.

Resources