Entity Helper Bundle
Introduction
Helper allows you to perform often required operations with entities which managed by Doctrine
., (*1)
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:, (*2)
composer require adrenalinkin/entity-helper-bundle
This command requires you to have Composer install globally., (*3)
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:, (*4)
<?php
// ...
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
$entityHelper = $container->get('linkin_entity_helper.helper');
// ...
Let's say we have an entity AcmeBundle\Entity\User
:, (*5)
<?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., (*6)
<?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., (*7)
<?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
., (*8)
getEntityClassShort
Returns short name of the received entity or class name., (*9)
<?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
., (*10)
getEntityIdNames
Returns an array of identifier field names numerically indexed., (*11)
<?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']
., (*12)
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., (*13)
<?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
., (*14)
getEntityIdValues
Returns an array of identifier values by received entity object., (*15)
<?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]
., (*16)
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., (*17)
<?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
., (*18)
Returns \Doctrine\ORM\Mapping\ClassMetadata
object for received entity or class name., (*19)
<?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
., (*20)
isManagedByDoctrine
Determines whether the requested class is under the control of Doctrine
., (*21)
<?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
., (*22)
License
, (*23)