PHP Method Doctrine\DBAL\Driver\Connection::commit Code ExamplesThis article is part of the series. The first article discussed the creation of an initial Symfony 2 project, the second is about Doctrine dbal commit forms, and the third article describes a nice and easy way to implement a REST Tren hormone side effects with the Symfony framework. Let's say that you have made some changes in the model of your Symfony project, you have committed the code, and it will be applied on the doctrine dbal commit. But, you also need to update the production database. You might run dbla command:
Doctrine\DBAL\Connection::commit PHP Method Code Examples - HotExamples
This article is part of the series. The first article discussed the creation of an initial Symfony 2 project, the second is about Symfony2 forms, and the third article describes a nice and easy way to implement a REST API with the Symfony framework.
Let's say that you have made some changes in the model of your Symfony project, you have committed the code, and it will be applied on the production. But, you also need to update the production database. You might run the command: Right and data safe way is to use Doctrine migrations. Doctrine can automatically generate migration for you, but that migration will contain the same SQL code like doctrine: To migrate the data, you will have to modify migration file, and we will talk about that later in the text.
Adjust the version if there is a newer one and run composer update or run composer install if you don't wish to update your other dependencies. But, doctrine takes care of all of that for you. Before executing the migration, you might wish to peek in the file just to check if the generated SQL is OK. If everything is fine, and there is no need to migrate the data we will come to it later , you can run the migration. Before going on, I would like to comment on migration class methods. By default, auto generated migration class which is contained in the migration file, and has same name as the file contains two methods: You can see it in the example below.
If migrating up, the field is added, and if migrating down, it is dropped. It runs all the new unexecuted migrations. Before running it, you can check what migrations are new, and see a lot of more info by executing this command:. The example shows a migration which moves the address fields from the person table to a separate table - address and references it in the person table.
The migration, as you can see it above, cares only for database structure, and doesn't care about the data. So, we will do it in these three steps:. Steps 1 and 3 already exist, and we just need to separate and order them properly. Step 2 is the tricky part, and we need to implement it. What exactly do we need to do in the step 2? We need to take the address data of each person and insert it in the address table. But the problem is that there can be 2 or more persons with the same address.
And each of them should have his copy of the address in the address table they would have only different id-s. So, the SQL above can't be used in this example. How to implement that? Here is how it should look like:. Read here more on mySQL cursors. In the postUp method we can even use entitymanager, but first we need to fetch it from the context.
So we need to have the context aware migration:. This is a different example than before. We are making the company table out of the string field work which is defined in the customer table. And those customers are actually employees in the company this doesn't make much sense now because you don't see complete model in these examples, but just accept it as it is. I must say right away that this example has an error! Another example of inconsistency between the code and the migration is when the migration is executed in the future.
Solution to this problem is simple: But the best solution is to use simple SQL queries if it is possible. Example of creating companies based on the customer. Very nice and helpful introduction to Doctrine Migrations.
I am currently working on a SaaS web app. It means all our customers share the same backend Symfony application but each one as its own database. Is it helpful to use Migrations in this case and why? Olivier at your place I would do separate the whole project like one Symfony application for only one Data Base because normally designing a Data base is a standalone project or sub-project and designing an application to communicate to is another one and most of the time the application structure data structure , logic algorithms , But according to your case if more than one customer have the same business logic they can share one DB then you can share the DB among them in this case but steel there is a probability that one day one of them ask for a feature that require change of DB schema and this might not be the desire of the other For this section where you execute the query, the results should actually be retrieved via a call to fetchAll like so: Mit der weiteren Nutzung unserer Seite stimmen Sie dem zu.
How to work with Doctrine migrations in Symfony. November Expert Topics. Please modify to your needs! Before running it, you can check what migrations are new, and see a lot of more info by executing this command: So, we will do it in these three steps: Copy all the address data from the person table to the address table.
Drop the address fields from person table. Here is how it should look like: We did these 3 sub steps: Create the stored procedure. So we need to have the context aware migration: Let's sum the important stuff: Stefan Galinski am Christian Elowsky am