PHP code example of talleu / php-redis-om

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);