From no framework to Symfony, Doctrine and dealing with migrations
When I first started using Symfony, I had already developed a social media app (github repository) through a Udemy Course and while it made me get into the practice of PHP writing, I wondered quite a few things:
1. How am I supposed to remember this file structure in ten months time? Is there any kind of universal criteria that we are not following?
2. How the hell do developers find a bug in a file with 400 lines of PHP + HTML
3. How am I supposed to see the flow of the code?
Obviously, as this is a guided programming course, questions were solved through subsequent videos or, through the course forum, but I still could not picture myself solving those bugs on my own. Moreover, as you will be able to see if you clicked on the github link (Developement branch) above mentioned, the mixture of languages along the files made me think that in order to be a developer you need to get a scanning app installed in your brain which would allow you to boost your reading in between the lines skills.
Summarizing, I finished my first app more discouraged than happy wondering whether I would be able to develop an app all by myself.
The first lesson I learned through developing my following project was that tools do make a difference. I'm not saying that any of the tools you are using are good or bad in essence. I do not recommend PHP Storm as your first IDE. It has plenty of possibilities, it will help you developing code through intuitive code suggestions and shortcuts, but if you are just starting, Sublime Text is a more suitable option probably, as it has way less features and it will allow you focusing on what is important in the first stages: writing code which works.
My second lesson was a little bit more annoying and boring: you need to learn how to use GIT. And if you are not to make the same mistakes I made, don't just learn the basics. Try to understand the consecuences of Commiting, Pushing and Pulling. Of course, everything can be undone, but sometimes it can make you lose a whole day trying to fix your mistakes.
As I entered the wonderful world of frameworks, I was amazed. Symfony was the most wonderful thing ever invented by the human being. Just one command and my register form was ready and working. And the same goes for login feature or the User entity. I felt like a mechanic using a screwdriver for the first time after having used his own hands for months. Doctrine was also a big advance:
php bin/console doctrine:migrations:generatephp bin/console doctrine:migrations:diff- First you draw the outline, the ears and the neck and take a picture of that day progress (that is, you migrate your entity to your database schema)
- Second day you add the "attributes" of eyes, nose and lips and take a beautiful photo of your progress
- On the third day you realize you didn't add the brows and the lashes, so you add them without taking a picture of that step (so that change is not migrated to your database schema)
- On the fourth day you decide to add a curlier shape to those lashes and make your brows bolder, and at the end of the day, you take the corresponding picture (that is, you migrate those changes to the database schema)
php bin/console doctrine:migrations:diffphp bin/console doctrine:migrations:migratephp bin/console doctrine:schema:update --force- Data are not migrated with all of the commands we have been mentioning. So how do you populate your database? First we need to distinguish between two cases:
- Cases in which interaction of the user will cause data to be persisted to the database, for example a registration feature. In that case the registration web service will take care of that everytime a user registers, so your app can work with an empty users database.
- Cases in which the app should start with a certain amount of data already persisted into the database in order to work. Think of a e-learning app where there's two kinds of users: administrators with access to creating lessons and exercises and students with access only to take the course. You will need the course already in the database if you are going to ask others to test your e-learning app, right? In that case, you will need to migrate your data by generating an empty migration first:
php bin/console doctrine:migrations:generate<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210623192926 extends AbstractMigration
{
public function getDescription() : string
{
return 'Here you can add a title to your migration';
}
public function up(Schema $schema) : void
{
$sqlKatasUpdated1 = <<<'EOD'
UPDATE `user` SET `name` = 'Laura' WHERE (`id` = '1');")
EOD;
$this->addSql($sqlKatasUpdated1);
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
}
}
Comments
Post a Comment