Entity

What is an Entity?

Every PHP object that you want to save in the database using Doctrine is called an "Entity" Show archive.org snapshot . Each entity must have a unique identifier.

Oro Platform Entity

The entity class resides in the Entity directory in the bundle's root. In VS Code, install PHP Setters & Getters to generate the setter and getter code.

src\Stars\Bundle\LoggerEventBundle\Entity\ActionData.php:

<?php

namespace Stars\Bundle\LoggerEventBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="sc_stars_logger_event_action_data")
 */
class ActionData
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="integer", name="event_id")
     */
    private $eventId;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $field;

    /**
     * @ORM\Column(type="string", length=4096)
     */
    private $from;

    /**
     * @ORM\Column(type="string", length=4096)
     */
    private $to;

    /**
     * Get the value of id
     */ 
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of id
     *
     * @return  self
     */ 
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of eventId
     */ 
    public function getEventId()
    {
        return $this->eventId;
    }

    /**
     * Set the value of eventId
     *
     * @return  self
     */ 
    public function setEventId($eventId)
    {
        $this->eventId = $eventId;

        return $this;
    }

    /**
     * Get the value of from
     */ 
    public function getFrom()
    {
        return $this->from;
    }

    /**
     * Set the value of from
     *
     * @return  self
     */ 
    public function setFrom($from)
    {
        $this->from = $from;

        return $this;
    }

    /**
     * Get the value of to
     */ 
    public function getTo()
    {
        return $this->to;
    }

    /**
     * Set the value of to
     *
     * @return  self
     */ 
    public function setTo($to)
    {
        $this->to = $to;

        return $this;
    }
}

See Doctrine Show archive.org snapshot for complete description on mapping.

Updating the Database Schema Show archive.org snapshot

We can update the DB with version as in:
src\Stars\Bundle\LoggerEventBundle\Migrations\Schema\v1_0\StarsLoggerEventBundle.php

<?php

namespace Stars\Bundle\LoggerEventBundle\Migraions\Schema\v1_0;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;

class StarsLoggerEventBundle implements Migration
{
    public function up(Schema $schema, QueryBag $queries)
    {
        $table = $schema->createTable('sc_stars_logger_event_action_data');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
        $table->addColumn('event_id', 'integer');
        $table->addColumn('field', 'string', ['length' => 64]);
        $table->addColumn('from', 'string', ['length' => 4096]);
        $table->addColumn('to', 'string', ['length' => 4096]);
        $table->setPrimaryKey(['id']);
        $table->addIndex(['event_id'], 'sc_stars_logger_event_action_data_event_id_idx', []);
        $table->addIndex(['field'], 'sc_stars_logger_event_action_data_field_idx', []);
    }
}

The code is executed with CLI in bash oro:migration:load.

Error While Migrating

[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:migration:load --dry-run
List of migrations:
  > Oro\Bundle\EntityExtendBundle\Migration\LoadEntityConfigStateMigration
  > Stars\Bundle\LoggerEventBundle\Migraions\Schema\v1_0\StarsLoggerEventBundle
  > Oro\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration
...
[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:migration:load --force
Process migrations...
  > Oro\Bundle\EntityExtendBundle\Migration\LoadEntityConfigStateMigration
  > Stars\Bundle\LoggerEventBundle\Migraions\Schema\v1_0\StarsLoggerEventBundle
    ERROR: Max table name length is 30. Please correct "sc_stars_logger_event_action_data" table in "Stars\Bundle\LoggerEventBundle\Migraions\Schema\v1_0\StarsLoggerEventBundle" migration
  > Oro\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration
...

See the code Show archive.org snapshot :

public function testWrongTableNameQuery()
{
	$migration = new WrongTableNameMigration();
	$migrations = [
		new MigrationState($migration)
	];
	$this->expectException('\RuntimeException');
	$this->expectExceptionMessage(
		'Failed migrations: Oro\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongTableNameMigration.'
	);
	$this->executor->executeUp($migrations);
	$this->assertEquals(
		'> Oro\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongTableNameMigration',
		$this->logger->getMessages()[0]
	);
	$this->assertEquals(
		sprintf(
			'  ERROR: Max table name length is %s. Please correct "%s" table in "%s" migration',
			$this->nameGenerator->getMaxIdentifierSize(),
			'extra_long_table_name_bigger_than_30_chars',
			get_class($migration)
		),
		$this->logger->getMessages()[1]
	);
}

The above error is fixed by Override an Override Service with Compiler Pass

Run Migration in CLI

[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:migration:load --force
Process migrations...
  > Oro\Bundle\EntityExtendBundle\Migration\LoadEntityConfigStateMigration
  > Stars\Bundle\LoggerEventBundle\Migraions\Schema\v1_0\StarsLoggerEventBundle
  > Oro\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration
  > ...

The table is created and be seen in PMA.

Managing Migration Version

The database migration from each bundle is stored in a table called oro_migrations. We can change to earlier version in column version to rerun the migration.

References

  1. Removing an Entity Show archive.org snapshot
kiatng About 5 years ago