PHP code example of krak / doctrine-util

1. Go to this page and download the library: Download krak/doctrine-util 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/ */

    

krak / doctrine-util example snippets




use function Krak\DoctrineUtil\{repoChunk, repoUntilEmpty};

$q = $em->createQuery('SELECT u FROM User u');
repoChunk($q, 100, function($users) {
    foreach ($users as $user) {
        // do something with user
    }
});

$q = $em->createQuery('SELECT u FROM User u WHERE u.email_sent = false');
repoUntilEmpty($q, 100, function($users) {
    foreach ($users as $user) {
        sendEmail($user);
        $user->setEmailSent(true);
    }
}, 1000); // run 1000 max


// extend the DoctrineUtil Entity Repository
class UserRepository extends Krak\DoctrineUtil\EntityRepository
{

}

// or set the default in the entity manager config
$em->getConfiguration()->setDefaultRepositoryClassName(Krak\DoctrineUtil\EntityRepository::class);

$userRepo = $em->getRepository(User::class);


// `get` alias for finding the entity or throw if not found
try {
    $user = $userRepo->get($id);
} catch (Doctrine\ORM\EntityNotFoundException $e) {

}


// Simple fluent interface

// find users where ids are in 1,2, and 3
$users = $userRepo->where(['id' => [1,2,3]])->find();

// retrieve a single user with the state and address entities loaded.
$user = $userRepo->with(['state', 'address'])->get(1);