Creating a Table in PIM by using ORM Doctrine

Pimcore is an extremely powerful tool, and one of our favorite representations of its power lies in Pimcore’s data modeling tool. It provides a wonderful GUI for creating classes with complex data structures. Supporting database tables and structures are automatically created, and developers don’t have to use SQL at all.

However, there are cases where you have to bypass Pimcore and use other ways to interact with the database. We can use Doctrine ORM for these cases, and this guide will teach you how to create a database table using Doctrine.

Installing necessary packages

 

Check if these packages exist with the following:

 

composer show symfony/orm-pack

composer show symfony/maker-bundle

 

If they do not exist, install them with these commands:

 

composer require symfony/orm-pack

composer require --dev symfony/maker-bundle

 

It’s important to use the “dev” environment for the maker bundle, which you can change in the “.env” file:
 

APP_ENV=dev

 

With the installed maker bundle, you should have access to the new set of commands under the namespace “make”. You can check if they exist with this command:

 

bin/console list make

 

If the bundle is successfully installed — but there aren’t any commands in the “make” namespace — it is possible that you need to register the bundle manually in “bundles.php”. Here is an example of the file:


<?php

return [
  Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];

 

Configure Doctrine ORM

 

Edit your “database.yaml” file. Here is an example:


doctrine:
  dbal: { connections: { default: { host: mysql, port: '3306', user: pimcore, password: pimcore, dbname: pimcore, mapping_types: { enum: string, bit: boolean }, server_version: 8.0.27 } } }
  orm:
      auto_mapping: true
      mappings:
          App:
              is_bundle: false
              dir: '%kernel.project_dir%/src/Entity'
              prefix: 'App\Entity'
              alias: App


To create an entity, simply run this command and follow the provided steps:

 

bin/console make:entity

 

Here is an example of a class “Customer” I created:

 

I have no name!@840f77d9db0e:/var/www/html$ bin/console make:entity
Cannot load Xdebug - it was already loaded

Class name of the entity to create or update (e.g. AgreeablePizza):
> Customer

created: src/Entity/Customer.php
created: src/Repository/CustomerRepository.php

Entity generated! Now let's add some fields!
You can always add more fields later manually or by re-running this command.

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

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

Field length [255]:
>

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

updated: src/Entity/Customer.php

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

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

Main Types
  * string
  * text
  * boolean
  * integer (or smallint, bigint)
  * float

Relationships/Associations
  * relation (a wizard 🧙 will help you build the relation)
  * ManyToOne
  * OneToMany
  * ManyToMany
  * OneToOne

Array/Object Types
  * array (or simple_array)
  * json
  * object
  * binary
  * blob

Date/Time Types
  * datetime (or datetime_immutable)
  * datetimetz (or datetimetz_immutable)
  * date (or date_immutable)
  * time (or time_immutable)
  * dateinterval

Other Types
  * ascii_string
  * decimal
  * guid
  * uuid
  * ulid


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

Field length [255]:
>

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

updated: src/Entity/Customer.php

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


         
  Success!

         

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

 

At this point, an entity class is created, as well as the Repository class with some basic methods.

Validate schema

 

bin/console doctrine:schema:validate --verbose

 

Running this command should return information about the correctness of mapping files and schema diff(s) detected (basically a list of SQL queries that will be executed).

Update schema

 

Finally, run this command to create a database table:

 

bin/console doctrine:schema:update --force

 

 

We hope that this guide has provided you with the needed solution. If you have any additional questions, reach out to us; we’ll be glad to help!

Looking for Exponential Growth? Let’s Get Started.
Explore next

Pimcore Tutorial

Check out our step-by-step tutorials on how to use Pimcore features and solutions.

Discover more