PHPackages                             eureka/component-orm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. eureka/component-orm

ActiveLibrary[Database &amp; ORM](/categories/database)

eureka/component-orm
====================

PHP orm library.

7.2.1(5mo ago)189632MITPHPPHP &gt;=8.3CI passing

Since Oct 9Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/eureka-framework/component-orm)[ Packagist](https://packagist.org/packages/eureka/component-orm)[ RSS](/packages/eureka-component-orm/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (10)Dependencies (12)Versions (56)Used By (2)

component-orm
=============

[](#component-orm)

[![Current version](https://camo.githubusercontent.com/36b1e512cf766f8eadea7e519f8cbc92d71aaa47930899ad240f0b383f740048/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f657572656b612f636f6d706f6e656e742d6f726d2e7376673f6c6f676f3d636f6d706f736572)](https://packagist.org/packages/eureka/component-orm)[![Supported PHP version](https://camo.githubusercontent.com/a6226f5200fbd8ca1dda8d38f953e4bc2be89e2ef5b9d38d66f014f9b4a3a121/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6f676f3d706870266c6162656c3d504850266d6573736167653d253345253344382e3326636f6c6f723d373737626234)](https://packagist.org/packages/eureka/component-orm)[![CI](https://github.com/eureka-framework/component-orm/workflows/CI/badge.svg)](https://github.com/eureka-framework/component-orm/workflows/CI/badge.svg)[![Quality Gate Status](https://camo.githubusercontent.com/006715b6bcfff9b28856c3185ce84ba8a62696dcf9484d133a33cfb95c90a95b/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d657572656b612d6672616d65776f726b5f636f6d706f6e656e742d6f726d266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=eureka-framework_component-orm)[![Coverage](https://camo.githubusercontent.com/e75fbbfd1dbdcbdf50bfc3ad2cbe5257bf0abaf1a2b7c77bc709493896c2bdc3/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d657572656b612d6672616d65776f726b5f636f6d706f6e656e742d6f726d266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=eureka-framework_component-orm)

PHP Simple ORM

Composer
--------

[](#composer)

To add this ORM to your project, you can use the following command:

```
~/path/to/your/project/$ composer require "eureka/component-orm"
```

You can install the component (for testing) with the following command:

```
make install
```

Update
------

[](#update)

You can update the component (for testing) with the following command:

```
make update
```

Configuration
-------------

[](#configuration)

If you are using Symfony Dependency Injection &amp; Yaml config, you can configure ORM with something look like this:

### Main orm.yaml config

[](#main-ormyaml-config)

```
parameters:

    #~ Comment part
    orm.comment.author:    'Eureka Orm Generator'
    orm.comment.copyright: 'My Copyright name'

    #~ Namespace base config
    orm.base_namespace.entity:     'Application\Domain' # Base namespace for entities
    orm.base_namespace.mapper:     'Application\Domain' # Base namespace for mapper (repository interface implementation)
    orm.base_namespace.repository: 'Application\Domain' # Base namespace for repository interfaces

    #~ Path base config
    orm.base_path.entity:     '%kernel.directory.root%/src/Domain' # Base path for entities
    orm.base_path.mapper:     '%kernel.directory.root%/src/Domain' # Base path for mapper (repository interface implementation)
    orm.base_path.repository: '%kernel.directory.root%/src/Domain' # Base namespace for repository interfaces

    #~ Cache base config
    orm.cache.enabled: false                  # Define globally if cache is enable or not
    orm.cache.prefix: 'website.magiclegacy.'  # Cache prefix for application database data

    #~ Validation - /!\ Require "eureka/component-validation" when validation is enabled
    orm.validation.enabled: true              # false: no validation of value in entities setter
    orm.validation.auto: true                 # set true to generate auto validation regarding tables columns definitions

    #~ Define list of available config.
    # Alias name will be used in "joins" config part
    orm.configs:
        #~ Usage
        #alias: '%orm.config.{name}%'
        #~ Core website
        user:                 '%orm.config.user%'

        #~ Core Blog
        blog_post:     '%orm.config.blog_post%'
        blog_category: '%orm.config.blog_category%'
        blog_tag:      '%orm.config.blog_tag%'
        blog_post_tag: '%orm.config.blog_post_tag%'
```

### Example table yaml config

[](#example-table-yaml-config)

```
# ORM Config file
parameters:

    orm.config.user:
        #~ Some meta data for phpdoc block comments
        comment:
            author:    '%orm.comment.author%'
            copyright: '%orm.comment.copyright%'

        #~ Namespace for generated files
        namespace:
            entity:     '%orm.base_namespace.entity%\User\Entity'
            mapper:     '%orm.base_namespace.mapper%\User\Infrastructure\Mapper'
            repository: '%orm.base_namespace.repository%\User\Repository'

        #~ Path for generated files
        path:
            entity:     '%orm.base_path.entity%/User/Entity'
            mapper:     '%orm.base_path.mapper%/User/Infrastructure/Mapper'
            repository: '%orm.base_path.repository%/User/Repository'

        #~ Cache configuration for this table
        cache:
            enabled:    '%orm.cache.enabled%'
            prefix:     '%orm.cache.prefix%user'

        #~ Table config
        database:
            table:      'user' # Name of the table
            prefix:     'user' # Fields prefix to remove in generated method. (Field user_id will have getId() method with this example)

        class:
            classname: 'User' # Name of the class (Do not set namespace here)

        #~ List of join configuration for "eager or lazy loading"
        joins:
            UserPosts:                   # Suffix name for setter/getter (here: getAllUserPosts() / setAllUserPosts())
                eager_loading: false     # set to true to allow eager loading
                config:   'blog_post'    # config alias name (see orm.yaml > "orm.configs" part)
                relation: 'many'         # one: Return unique entity | many: return list of all found entities
                type:     'inner'        # inner, left, right
                keys:
                    user_id: true        # key(s) for join. set "true" when column name is same in both table

            UserAddress:                 # Suffix name for setter/getter (here: getUserAddress() / setUserAddress())
                eager_loading: true      # set to true to allow eager loading
                config:   'user_address' # config alias name (see orm.yaml > "orm.configs" part)
                relation: 'one'          # one: Return unique entity | many: return list of all found entities
                type:     'inner'        # inner, left, right
                keys:
                    user_id: user_id     # You also can mapping name (useful when name differ in both table)

        #~ Validation configuration
        # /!\ Require "eureka/component-validation" when validation is enabled
        validation:
            enabled: '%orm.validation.enabled%'
            auto:    '%orm.validation.auto%'

            #~ Optional
            extended_validation:
                #~ Define or override validation config for some field if needed
                #~ Example
                user_name:
                    #type: string - optional when auto validation is enabled

                    #~ options values are merged with auto validation values.
                    #~ If any value is already defined with auto validation, this value override auto validation value
                    options:
                        min_length: 5
```

### Generator

[](#generator)

You must generate all ORM file with following command:

```
~/path/to/your/project/$ bin/console Orm/Script/Generator
```

> /!\\ This command require to have "eureka/component-console" installed &amp; bin/console script in your application (with a kernel-console)

Entity
------

[](#entity)

Entities are "reflection" of the table data. When you retrieve data from database, each entity is representation of one row of corresponding table.

Entity should never been instantiated directly. To get a new entity, you should use the following mapper method:

```
/** @var \Symfony\Component\DependencyInjection\Container $container */
$postRepository = $container->get('post.mapper');
/** @var \Application\Domain\Blog\Infrastructure\Mapper\PostMapper $postRepository */
$post = $postRepository->newEntity();
```

Mapper
------

[](#mapper)

When you need to retrieve data from database, you have to add a method in mapper to retrieve data, commonly prefixed by `find` or `findAll` (according to retrieve one or many entities).

### Mapper Example

[](#mapper-example)

Retrieve 10 latest blog posts (as entities)

```
