PHP code example of yapro / doctrine-ext

1. Go to this page and download the library: Download yapro/doctrine-ext library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yapro / doctrine-ext example snippets


$data = [
    'name'        => 'Fred Jones',
    'email'       => '[email protected]',
    'company'     => 2,
    'permissions' => [1, 2, 3, 4]
];

$hydrator = new \YaPro\DoctrineExt\Hydrator\ArrayHydrator($entityManager);
$entity   = $hydrator->hydrate('App\Entity\User', $data);

$data = [
    'attributes'    => [
        'name'  => 'Fred Jones',
        'email' => '[email protected]',
    ],
    'relationships' => [
        'company'     => [
            'data' => ['id' => 1, 'type' => 'company'],
        ],
        'permissions' => [
            'data' => [
                ['id' => 1, 'type' => 'permission'],
                ['id' => 2, 'type' => 'permission'],
                ['id' => 3, 'type' => 'permission'],
                ['id' => 4, 'type' => 'permission'],
                ['name' => 'New permission']
            ]
        ]
    ]
];
    
$hydrator = new \YaPro\DoctrineExt\Hydrator\JsonApiHydrator($entityManager);
$entity   = $hydrator->hydrate('App\Entity\User', $data);

$json = '{
   "parentId": 12, 
   "title": "title1", 
   "comments": [{"parentId": 23, "message": "str1"}, {"parentId": 34, "message": "str2"}]
}';
$hydrator = new \YaPro\DoctrineExt\Hydrator\SimpleHydrator($entityManager, new \YaPro\Helper\JsonHelper());
$entity   = $hydrator->fromJson(Article::class, $json);


use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use YaPro\DoctrineExt\ReloadDatabaseTrait;

class ExampleClassTest extends KernelTestCase
{
    use ReloadDatabaseTrait;

    protected static EntityManagerInterface $entityManager;

    public static function setUpBeforeClass()
    {
        self::$entityManager = self::$container->get(EntityManagerInterface::class);
    }
    
    public function myTest()
    {
        $this->truncateClass('My\User');
        $this->truncateTable('user_orders');
        $this->truncateAllTables();
        
        // ... some useful actions
    }
}



namespace App\Entity;
class MyEntity
{
    #[ORM\Column(type: Types::BIGINT)]
    private int $mybigint = 0;

$items = $this->getEntityManager()->getConnection()->fetchAll("
    SELECT 
        id,
        title,
        createdAt
    FROM Article
    WHERE isShow = 1
    ORDER BY createdAt DESC
    LIMIT 20, 10
");

// get the total number of items like: SELECT COUNT(*) FROM Article WHERE isShow = 1
echo $this->getEntityManager()->getConnection()->fetchColumn(DBALConnectionWrapper::SELECT_FOUND_ROWS);
// if you use TotalItemsTrait you can call:
echo $this->getTotalItems();
sh
wget https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/download/v3.61.1/php-cs-fixer.phar && chmod +x ./php-cs-fixer.phar
./php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php -v --using-cache=no --allow-risky=yes