Categories
Software Development Web Development

Symfony doctrine remove column from entity table

As a newcomer to Doctrine I had to google and dig to figure this out. Explanation here.

Basically if you want to remove a column from a table when using Doctrine and Symfony Entities all you do is remove the variable and the matching getter/setter methods from the Entity, then run a diff.

php bin/console doctrine:migrations:diff

instead of making a migration, this command makes the migration file for you. Then you can run this command to change the table.

php bin/console doctrine:migrations:migrate

What is happening is Doctrine ORM is used to map the Database tables to an Entity. Every Doctrine/Symfony Entity matches 1 table in a database.

Lets view an example. This Entity should be familiar to anyone who created an app using the Symfony maker system. User.php should be located in app\src\Entity\User.php and it looks basically like this.


<?php namespace App\Entity; use App\Repository\UserRepository; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; /** * User * * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})}, indexes={@ORM\Index(name="email", columns={"email"})}) * @ORM\Entity(repositoryClass=UserRepository::class) */ class User implements UserInterface { /** * @var int * * @ORM\Column(name="id", type="bigint", nullable=false, options={"unsigned"=true}) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private int $id; /** * @var string * * @ORM\Column(name="email", type="string", length=255, nullable=false) */ private string $email; /** * @var string * * @ORM\Column(name="password", type="string", length=255, nullable=false) */ private string $password; /** * @var array|null * * @ORM\Column(name="roles", type="json", nullable=true) */ private ?array $roles; /** * @var string|null * * @ORM\Column(name="web_token", type="string", length=255, nullable=true) */ private ?string $webToken; /** * @var string|null * * @ORM\Column(name="api_token", type="string", length=255, nullable=true) */ private ?string $apiToken; public function getId(): ?string { return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getRoles(): ?array
    {
        return $this->roles;
    }

    public function setRoles(?array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    public function getWebToken(): ?string
    {
        return $this->webToken;
    }

    public function setWebToken(?string $webToken): self
    {
        $this->webToken = $webToken;

        return $this;
    }

    public function getApiToken(): ?string
    {
        return $this->apiToken;
    }

    public function setApiToken(?string $apiToken): self
    {
        $this->apiToken = $apiToken;

        return $this;
    }


    public function getSalt()
    {
        // TODO: Implement getSalt() method.
    }

    public function getUsername(): ?string
    {
        // TODO: Implement getUsername() method.
        return $this->getEmail();
    }

    public function eraseCredentials()
    {
        // TODO: Implement eraseCredentials() method.
    }
}

As you can see I have a web token and api token in this class. If I wanted to remove the web token from my database I simply remove the relate variable and getter/setters like so.


<?php namespace App\Entity; use App\Repository\UserRepository; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; /** * User * * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})}, indexes={@ORM\Index(name="email", columns={"email"})}) * @ORM\Entity(repositoryClass=UserRepository::class) */ class User implements UserInterface { /** * @var int * * @ORM\Column(name="id", type="bigint", nullable=false, options={"unsigned"=true}) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private int $id; /** * @var string * * @ORM\Column(name="email", type="string", length=255, nullable=false) */ private string $email; /** * @var string * * @ORM\Column(name="password", type="string", length=255, nullable=false) */ private string $password; /** * @var array|null * * @ORM\Column(name="roles", type="json", nullable=true) */ private ?array $roles; /** * @var string|null * * @ORM\Column(name="api_token", type="string", length=255, nullable=true) */ private ?string $apiToken; public function getId(): ?string { return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getRoles(): ?array
    {
        return $this->roles;
    }

    public function setRoles(?array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

   

    public function getApiToken(): ?string
    {
        return $this->apiToken;
    }

    public function setApiToken(?string $apiToken): self
    {
        $this->apiToken = $apiToken;

        return $this;
    }


    public function getSalt()
    {
        // TODO: Implement getSalt() method.
    }

    public function getUsername(): ?string
    {
        // TODO: Implement getUsername() method.
        return $this->getEmail();
    }

    public function eraseCredentials()
    {
        // TODO: Implement eraseCredentials() method.
    }
}

Now when you run the migrations:diff command a new migration file will be created. Inside that file will be the queries to make the changes in the “up” method of that migrations file. In the “down” method you will find the code to restore the database and add the column back, which will also update the Entity class.

After you run the diff you run a migration. Running the migration changes your database to match your changes in your Entity class.

Categories
Resources Software Development

Random Links about Databases

Why Uber dropped PostgreSQL – interesting article if you are thinking about using PostgreSQL

https://db-engines.com/en/ranking – a really cool website that lists database engines by popularity check it out.

Cassandra Use Cases: When To Use And When Not To Use Cassandra – really good article about when to use and not use Cassandra Db

7 mistakes when using Apache Cassandra – Don’t make these mistakes if you want to use Cassandra database

Categories
Random Resources

Scala database access driver client links and resources.

Resources and links about accessing a database in Scala.

An async postgre mysql db access client 

Categories
Resources Software Development

Scala Play Framework Slick resources and links

Below is a list of resources, links, videos etc. a

Play Framework and Slick example updated – very helpful article

Categories
Resources Software Development

HikariCP connection pooling software resources and links

HikariCP is a connection pool software that maintains connection pool resources so your code use them to access databases.

This can be used in place of Akka and Scala execution context for database connections. This helps keep your main application code from being blocked while it does a database query to either store or retrieve information.

HikariCP is the fastest and most resource efficient connection pool technology for the JVM.

 

Links

Introduction to HikariCP – a great introduction to what HikariCP is to start out with.

HikariCP documentation – a link to the actual documentation, you have to click the version of CP you want to use.

A quick guide to connection pooling in Java

Categories
Random Resources Software Development

Interesting blog about databases

I found this link somehow. This guy has all kinds of interesting information about databases, programming and technology in general . https://aphyr.com/tags/jepsen

Categories
Resources Software Development

Links about using MySQL as a NoSQL database

I got interested in using the JSON field of MySQL so I did some googling turns out I am not the only one. Some things have changed over the years since MySQL introduced the JSON field.

MySQL is a Great NoSQL Datastore

Scaling to 100M: MySQL is a Better NoSQL

Using MySQL as a document store – from the documentation

Categories
Resources

Lagom notes and facts

This is just a list of notes and facts I have gathered about Lagom. I’m the kind of person that asks 1 million questions and likes to know why things are done.

Why does Lagom use Cassandra as an event store system?

Lagom uses Cassandra as it’s event store. I wanted to know why they would choose Cassandra so I did some digging.  I found the following video helpful in understanding why anyone would use Cassandra for an event store instead of EventStore itself.  The video is not about Lagom, however it is about Cassandra and CQRS/Event Sourcing and why you would choose Cassandra as an event store. Basically Cassandra has features that are required for Event Sourcing and make it a good fit for it.

Why does Lagom use KAFKA as a Message Broker?

The documentation mentions using KAFKA as a Message Broker. I wanted to know why?  This article helped clarify what the difference between a message bus and a message broker.  Basically a broker gives a little less coupling with some other nice features. A message bus is more strict with a Schema like a traditional RDBMS database system, a message broker is more free like a NoSql database it has no Schema. Read the article for the other benefits and differences.

Codecentric: CQRS and Event Sourcing Applications with Cassandra

This video explains how to handle event sourcing with Cassandra

I also found this link to be helpful in understanding why Lagom would use Cassandra instead of EventStore. That article has a nice comparison of Cassandra vs other similar software used for event storage. A quick look at the features is all it takes.