Developed with love by KnpLabs Hire us for your project!
15

entity-helper-bundle

by adrenalinkin

Symfony bundle. Provides help for the work with entities which managed by Doctrine

Entity Helper Bundle На Русском

Introduction

Helper allows you to perform often required operations with entities which managed by Doctrine.

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable
version of this bundle:
text
composer require adrenalinkin/entity-helper-bundle

This command requires you to have Composer install globally.

Step 2: Enable the Bundle

<?php
// app/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

/**
 * The Kernel is the heart of the Symfony system
 */
class AppKernel extends Kernel
{
    /**
     * {@inheritdoc}
     */
    public function registerBundles()
    {
        $bundles = [
            // ...

            new Linkin\Bundle\EntityHelperBundle\LinkinEntityHelperBundle(),
        ];

        return $bundles;
    }

    /**
     * {@inheritdoc}
     */
    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

Usage

Get entity helper by dependencies container:

<?php

// ...

/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

// ...

Let's say we have an entity AcmeBundle\Entity\User:

<?php
// AcmeBundle\Entity\User.php

// ...
class User
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer", name="id_user")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", nullable=false)
     */
    private $password;

    /**
     * @ORM\Column(type="string", nullable=false, length=50, unique=true)
     *
     * @var string
     */
    private $username;

    // ...
}

createEntity

Create instance of the class, which managed by Doctrine, by received class name.

<?php

// ...

/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

// Create empty User entity
$user = $entityHelper->createEntity(User::class);

// Create empty User entity by received short name
$user = $entityHelper->createEntity('AcmeBundle:User');

// Create User entity and fill some fields
$user = $entityHelper->createEntity('AcmeBundle:User', ['id' => 1, 'username' => 'acme-login']);

// Create User entity and fill identity field in that case when you don't know the name of the identity field
foreach (['AcmeBundle:User', 'AcmeBundle:Role'] as $className) {
    $object = $entityHelper->createEntity($className, [EntityHelper::IDENTITY => 1]);
}

getEntityClassFull

Returns full name of the received entity or class name.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$className = $entityHelper->getEntityClassFull(User::class);
$className = $entityHelper->getEntityClassFull('AcmeBundle:User');
$className = $entityHelper->getEntityClassFull($user);

In the all cases will be return string value: AcmeBundle\Entity\User.

getEntityClassShort

Returns short name of the received entity or class name.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$className = $entityHelper->getEntityClassShort(User::class);
$className = $entityHelper->getEntityClassShort('AcmeBundle\Entity\User');
$className = $entityHelper->getEntityClassShort($user);

In the all cases will be return string value: AcmeBundle:User.

getEntityIdNames

Returns an array of identifier field names numerically indexed.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$names = $entityHelper->getEntityIdNames(User::class);
$names = $entityHelper->getEntityIdNames('AcmeBundle\Entity\User');
$names = $entityHelper->getEntityIdNames('AcmeBundle:User');
$names = $entityHelper->getEntityIdNames($user);

In the all cases will be return array value: ['id'].

getEntityIdName

Returns single identifier field name by received entity object or class name.
Important: In that case when entity have several identifier names method will be return only first identifier name.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$idName = $entityHelper->getEntityIdName(User::class);
$idName = $entityHelper->getEntityIdName('AcmeBundle\Entity\User');
$idName = $entityHelper->getEntityIdName('AcmeBundle:User');
$idName = $entityHelper->getEntityIdName($user);

In the all cases will be return string value: id.

getEntityIdValues

Returns an array of identifier values by received entity object.

<?php

// ...

/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');
/** @var \AcmeBundle\Entity\User $user */
$user = $entityHelper->createEntity('AcmeBundle:User', ['id' => 1]);

$idValues = $entityHelper->getEntityIdValues($user);

Will be return array value: [1].

getEntityIdValue

Returns single identifier field value by received entity object.
Important: In that case when entity have several identifier method will be return only first identifier field value.

<?php

// ...

/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');
/** @var \AcmeBundle\Entity\User $user */
$user = $entityHelper->createEntity('AcmeBundle:User', ['id' => 1]);

$idValue = $entityHelper->getEntityIdValues($user);

Will be return numeric value: 1.

getEntityMetadata

Returns \Doctrine\ORM\Mapping\ClassMetadata object for received entity or class name.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$metaData = $entityHelper->getEntityMetadata(User::class);
$metaData = $entityHelper->getEntityMetadata('AcmeBundle\Entity\User');
$metaData = $entityHelper->getEntityMetadata('AcmeBundle:User');
$metaData = $entityHelper->getEntityMetadata($user);

In the all cases will be return object value: \Doctrine\ORM\Mapping\ClassMetadata.

isManagedByDoctrine

Determines whether the requested class is under the control of Doctrine.

<?php

// ...

/** @var \AcmeBundle\Entity\User $user */
$user = new User();
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');

$isManaged = $entityHelper->isManagedByDoctrine(User::class);
$isManaged = $entityHelper->isManagedByDoctrine('AcmeBundle\Entity\User');
$isManaged = $entityHelper->isManagedByDoctrine('AcmeBundle:User');
$isManaged = $entityHelper->isManagedByDoctrine($user);

In the all cases will be return bool value: true.

License

license

Copyright (c) Viktor Linkin since 2014

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
linkin_entity_helper:  []