Skip to content

cyrilverloop/doctrine-entities

Repository files navigation

doctrine-entities

Some default doctrine entity/fields requiring PHP >=8.2 and Doctrine ORM >=2.17.

For compatibility with PHP <8.2 and Doctrine ORM <2.17, see version <7.0 of this software.

This includes :

  • IntId : an abstract class with an $id as an integer identifier/primary key;
  • Available : a trait that adds an $available boolean field;
  • Priority : a trait that adds a $priority integer field;
  • Slug : a trait that adds a $slug string field;
  • ConnectedAt : two traits that add a $connectedAt field (choose between \DateTime() or \DateTimeImmutable());
  • CreatedAt : two traits that add a $createdAt field (choose between \DateTime() or \DateTimeImmutable());
  • UpdatedAt : two traits that add a $updatedAt field (choose between \DateTime() or \DateTimeImmutable()).

License Type coverage Minimum PHP version

Installation

As a Composer depedency

In your project directory run

user@host project$ composer require "cyril-verloop/doctrine-entities"

For development purposes

user@host ~$ cd [PATH_WHERE_TO_PUT_THE_PROJECT] # E.g. ~/projects/
user@host projects$ git clone https://github.com/cyrilverloop/doctrine-entities.git
user@host projects$ cd doctrine-entities
user@host doctrine-entities$ composer install -o
user@host doctrine-entities$ phive install --trust-gpg-keys 4AA394086372C20A,12CE0F1D262429A5,31C7E470E2138192,8AC0BAA79732DD42,C5095986493B4AA0

Mappings

There are annotations, attributes and XML mappings. For XML mappings, read below.

Entity

The XML file is located in the config/doctrine/ directory. You just have to copy or reference it depending on your needs. If you use Symfony, the XML file just needs to be referenced in the resources/config/packages/doctrine.yaml file :

doctrine:
    orm:
        entity_managers:
            app: # your project name.
                mappings:
                    CyrilVerloop\DoctrineEntities:
                        type: xml
                        dir: '%kernel.project_dir%/vendor/cyril-verloop/doctrine-entities/config/doctrine'
                        prefix: CyrilVerloop\DoctrineEntities

Traits

You need to copy the <field /> fields you require in your XML files.

For example :

<field name="connectedAt" column="connected_at" type="datetime" nullable="true" />
<field name="connectedAt" column="connected_at" type="datetime_immutable" nullable="true" />
<field name="createdAt" column="created_at" type="datetime" />
<field name="createdAt" column="created_at" type="datetime_immutable" />
<field name="updatedAt" column="updated_at" type="datetime" nullable="true" />
<field name="updatedAt" column="updated_at" type="datetime_immutable" nullable="true" />
<field name="available" column="available" type="boolean" />
<field name="priority" column="priority" type="smallint">
    <options>
        <option name="default">0</option>
    </options>
</field>
<field name="slug" column="slug" type="string" nullable="true" />

You can also look at the resources/mappings/Example.orm.xml file.

Usage

IntId

If your entities need an integer as an identifier/primary key, they can extend the mapped super class CyrilVerloop\DoctrineEntities\IntId.

<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\IntId;

class Product extends IntId
{
    // Your code here.
}

Available / Priority / Slug / Timestampable ...

If your entities need some other fields, they can use a trait.

<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\Available;

class Product
{
    use Available;

    // Your code here.
}

Continuous integration

Tests

To run the tests :

user@host doctrine-entities$ ./tools/phpunit -c ./ci/phpunit.xml

The generated outputs will be in ./ci/phpunit/. Look at ./ci/phpunit/html/index.html for code coverage and ./ci/phpunit/testdox.html for a verbose list of passing / failing tests.

To run mutation testing, you must run PHPUnit first, then :

user@host doctrine-entities$ ./tools/infection -c./ci/infection.json

The generated outputs will be in ./ci/infection/.

Static analysis

To do a static analysis :

user@host doctrine-entities$ ./tools/psalm -c ./ci/psalm.xml [--report=./psalm/psalm.txt --output-format=text]

Use "--report=./psalm/psalm.txt --output-format=text" if you want the output in a file instead of on screen.

PHPDoc

To generate the PHPDoc :

user@host doctrine-entities$ ./tools/phpdocumentor --config ./ci/phpdoc.xml

The generated HTML documentation will be in ./ci/phpdoc/.

Standard

All PHP files in this project follows PSR-12. To indent the code :

user@host doctrine-entities$ ./tools/phpcbf --standard=PSR12 --extensions=php -p ./src/ ./tests/

About

Some default doctrine entity/fields requiring PHP 8.2+ and Doctrine 2.17+.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages