Symfony 4.2 - Creating an entity with the maker-bundle and migrate it

jerdox

JerDox

Posted on June 21, 2022

Symfony 4.2 - Creating an entity with the maker-bundle and migrate it

After I installed the API-Platfrom in my last Post, I'd like to create an entity which I want to used for a new web service.

My setup is:

  • php 7.1.3
  • Symfony 4.2 (later 4.4)
  • API-Platfrom/core 2.1
  • maker-bundle 1.12
  • Doctrine ORM 2.7

Behind the scenes I used the MS SQL Server 2016 as Database.

With the maker-bundle it should be easy to create a new entity by using composer.

So let's start. In the Terminal run:

php bin/console make:entity

After pressing Enter the Terminal will ask you some Question to create that entity.

Q1:
Class name of the entity to create or update (e.g. DeliciousPuppy):
-> apiTest

Q2:
Mark this class as an API Platform resource (expose a CRUD API for it) (yes/no) [no]:
-> that's comes because we installed the API-Platfrom. So I want it to be an API resource I answer with "yes"

... and then error...

In DoctrineHelper.php line 314:

  Warning: Invalid argument supplied for foreach()                                                      
Enter fullscreen mode Exit fullscreen mode

Okay, because I have no idea what the problem is I googeling and found that this can be happen because of an older version of the maker-bundle. (Post a GitHub Issue - Link)

I take a look on Packagist.org to find the lastest version which I can use.

I decide to update to version 1.35 of the maker-bundle because it works with my php version 7.1.3 and also it's the last version which require Symfony version 4.0 later versions using Symfony 4.4.

So in my composer.json I changed the version of the maker-bundle from "^1.12" to "^1.35.0".

Digression "^"-operator
If I understand the documentation for the versioning of bundles in the composer.json right - and I not sure I do - (Link)
the "^"-operator is to accept a version range of this bundle to the next versioning level which I have specified. I my case I think "^1.35.0" is the range of "1.35.0 to < 1.36"

Okay back on track I run:

composer update --with-all-dependencies

... it runs and ends successfully, but strange, composer told me:

...
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Enter fullscreen mode Exit fullscreen mode

So it seems that composer did not make any changes. To get the information which version is installed I run

composer show

Which tells me that the maker-bundle is installed in version 1.35:

...
symfony/maker-bundle  v1.35.0   Symfony Maker
...
Enter fullscreen mode Exit fullscreen mode

... so it seems to me that it was already installed in version 1.35. I take a look in the composer.lock file of a backup and yes it already was on version 1.35 before I updated it. In the future I should check the versions before make a change in the composer.json

So that's not solves my problem with creating an entity.

Next I try the default action and clear the composer cache with:

composer clearcache

... makes not different, still the same error by creating an entity...

Found the next article which refers to this error (GitHub).
... at the end I think need to update the maker-bundle to version 1.36.
But for me it seems that I also have to update my Symfony version to 4.4 as a requirement of the maker-bundle 1.36.

So because I don't see another way to solve my problem I'll update my Symfony version to 4.4.
Post: Updateing Symfony 4.2 to 4.4

After I has updated my Symfony version to 4.4, I see that also the maker-bundle was updated to version 1.39.0, so hopefully this has solved my problem too.

Let's figure out by running again:

php bin/console make:entity

This time after Q2: it dose not drops an error, yeah...

So let's go-ahead and creating some properties "name" and "age"

Q3:
New property name (press <return> to stop adding fields):
-> name

Field type (enter ? to see all types) [string]:
-> string

Field length [255]:
-> 255

Can this field be null in the database (nullable) (yes/no) [no]:
->

Add the other property "age"

Q4:
Add another property? Enter the property name (or press <return> to stop adding fields):
-> age

Field type (enter ? to see all types) [string]:
-> integer

Can this field be null in the database (nullable) (yes/no) [no]:
-> no

Then exit the dialog by clicking enter.

At the end I get the message

Success! 


Next: When you're ready, create a migration with php bin/console make:migration

Enter fullscreen mode Exit fullscreen mode

So let's do that with the command:

php bin/console make:migration

If the user you used with doctrine to access your database did not have the right to create a table you will get the following error message:

In DBALException.php line 185:

  An exception occurred while executing 'CREATE TABLE migration_versions (version NVARCHAR(14) NOT NULL, executed_at DATETIME NOT NULL, PRIMARY KEY (version))':  

  SQLSTATE [42000, 262]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CREATE TABLE permission denied in database 'BCMS_FERON'.                           


In Error.php line 45:

  SQLSTATE [42000, 262]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CREATE TABLE permission denied in database '<dbName>'.  
Enter fullscreen mode Exit fullscreen mode

After I extend the permission for this user the command runs successfully, perfect.

And as before in the terminal we find the message what we should do next:

 Success! 


 Next: Review the new migration "src/Migrations/Version20220621131505.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
Enter fullscreen mode Exit fullscreen mode

Okay I go on an run the recommended command:

php bin/console doctrine:migrations:migrate

After confirm the question:

WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)
Enter fullscreen mode Exit fullscreen mode

which I answering with "yes", it runs and fails.

Looks like doctrine has a problem with parts of the structure of the database. Because I didn't want to figure out what exactly the problem is, I decide to reduce the created migration-file (you can find it at src/Migrations/Version.php) in a way that only the entries for the new entity "apiTest" is still inside.

So in the end in the migration-file is nothing more then create-SQL statement for this new table:

CREATE TABLE api_test (id INT IDENTITY NOT NULL, name NVARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (id))

(you can easily go directly into the database and create the table with this statement by your self)

After that change, the command:
php bin/console doctrine:migrations:migrate
runs without an error.

And we done - creating an entity with the maker-bundle and migrate it to the database.

💖 💪 🙅 🚩
jerdox
JerDox

Posted on June 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related