1. Go to this page and download the library: Download talleu/php-redis-om 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/ */
talleu / php-redis-om example snippets
use Talleu\RedisOm\Om\Mapping as RedisOm;
#[RedisOm\Entity]
class User
{
#[RedisOm\Id]
#[RedisOm\Property]
public int $id;
#[RedisOm\Property(index:true)]
public string $name;
#[RedisOm\Property]
public \DateTimeImmutable $createdAt;
}
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Talleu\RedisOm\Om\RedisObjectManagerInterface;
use App\Entity\Book;
class MySymfonyController extends AbstractController
{
public function __construct(private RedisObjectManagerInterface $redisObjectManager)
{}
#[Route('/', name: 'app_home')]
public function index(): Response
{
$book = new Book();
$book->name = 'Martin Eden';
$this->redisObjectManager->persist($book);
$this->redisObjectManager->flush();
//..
}
}
use Talleu\RedisOm\Om\RedisObjectManager;
$user = new User()
$user->id = 1;
$user->name = 'John Doe';
// Persist the object in redis
$objectManager = new RedisObjectManager();
$objectManager->persist($user);
$objectManager->flush();
// Retrieve the object from redis
$user = $this->redisObjectManager->find(User::class, 1);
$user = $this->redisObjectManager->getRepository(User::class)->find(1);
$user = $this->redisObjectManager->getRepository(User::class)->findOneBy(['name' => 'John Doe']);
// Retrieve a collection of objects
$users = $this->redisObjectManager->getRepository(User::class)->findAll();
$users = $this->redisObjectManager->getRepository(User::class)->findBy(['name' => 'John Doe'], ['createdAt' => 'DESC'], 10);
enum Status: string
{
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}
#[RedisOm\Entity]
class Task
{
#[RedisOm\Id]
#[RedisOm\Property]
public int $id;
#[RedisOm\Property(index: true)]
public Status $status;
}
#[RedisOm\Entity]
class User
{
#[RedisOm\Id]
#[RedisOm\Property]
public int $id;
#[RedisOm\Property(index: true)]
#[RedisOm\Unique]
public string $email;
}
#[RedisOm\Entity]
#[RedisOm\Unique(properties: ['username', 'tenantId'])]
class User
{
#[RedisOm\Id]
#[RedisOm\Property]
public int $id;
#[RedisOm\Property]
public string $username;
#[RedisOm\Property]
public int $tenantId;
}
use Talleu\RedisOm\Exception\UniqueConstraintViolationException;
try {
$objectManager->persist($user);
$objectManager->flush();
} catch (UniqueConstraintViolationException $e) {
// $e->getMessage() describes the conflicting field(s) and value(s)
}
$repository = $objectManager->getRepository(User::class);
// Delete all inactive users — unique-constraint keys are cleaned up automatically
$deleted = $repository->bulkDelete(['status' => 'inactive']);
// Update a scalar field on many objects at once
$updated = $repository->bulkUpdate(['country' => 'FR'], ['currency' => 'EUR']);
// Via repository — full control
foreach ($repository->stream(['status' => 'active'], batchSize: 500) as $user) {
// process $user — break works normally
}
// Via object manager — identity map is cleared automatically between batches
foreach ($objectManager->stream(User::class, ['status' => 'active']) as $user) {
// process $user
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.