PHP code example of ivanvoitovych / redis-repository

1. Go to this page and download the library: Download ivanvoitovych/redis-repository 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/ */

    

ivanvoitovych / redis-repository example snippets


use RedisDb\Models\BaseEntity;

class BlogPost extends BaseEntity
{
    public string $Title;
    public string $SeoTitle;
    public string $Body;
    public string $Author;
    public int $DisplayOrder = 0;
    public bool $Published = false;
    public int $Group = 0;
}

use RedisDb\Models\BaseEntityDbMap;

class BlogPostDbMap extends BaseEntityDbMap
{
    public const ORDER_BY_ORDER = 'DisplayOrder';

    public const PROPERTY_Published = 'Published';
    public const PROPERTY_Group = 'Group';
    public const PROPERTY_SeoTitle = 'SeoTitle';

    static array $Indexes = [
        'SeoTitle_UX' => [
            'Unique' => true,
            'Properties' => [self::PROPERTY_SeoTitle]
        ],
        'Published_IX' => [
            'Unique' => false,
            'Properties' => [self::PROPERTY_Published]
        ],
        'Published-Group_IX' => [
            'Unique' => false,
            'Properties' => [
                self::PROPERTY_Group,
                self::PROPERTY_Published
            ]
        ]
    ];

    static array $Positions = [
        self::ORDER_BY_ORDER => [
            'Properties' => [self::ORDER_BY_ORDER]
        ]
    ];
}

$mapper = new StdObjectMapper();
$connector = new RedisConnector();

$repository = new BaseRepository($connector, $mapper, BlogPost::class, BlogPostDbMap::class);

$blog = new BlogPost();
$blog->Author = 'Miki the black cat';
$blog->Body = 'body sample';
$blog->DisplayOrder = $i;
$blog->Group = $i % 20;
$blog->Published = $i % 2 === 1;
$blog->SeoTitle = "blog-number-$i"; // unique
$blog->Title = "Blog $i";
$repository->Create($blog);

$repository->Update($blog);

$repository->Delete($blog);

$repository->GetList(1, 2, BaseEntityDbMap::ORDER_BY_CreatedOn, 1);
$repository->GetList(1, 2, BlogPostDbMap::ORDER_BY_ORDER, 0);

$repository->GetByKeys(
    [
        BlogPostDbMap::PROPERTY_Group => $group,
        BlogPostDbMap::PROPERTY_Published => $group % 2 === 1
    ],
    1,
    5,
    1,
    BlogPostDbMap::ORDER_BY_ORDER
    );