Skip to content

Commit

Permalink
improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
clementtalleu committed Jun 6, 2024
1 parent e85268a commit 0e9976d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: Test cs-fixer
run: vendor/bin/php-cs-fixer fix src --dry-run --diff --no-ansi
- name: Run phpstan
run: vendor/bin/phpstan analyse src --level=3
run: vendor/bin/phpstan analyse src --level=5
- name: Run tests
run: vendor/bin/phpunit
- name: composer audit
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

[![Build Status](https://github.com/clementtalleu/php-redis-om/actions/workflows/tests.yaml/badge.svg)](https://github.com/clementtalleu/php-redis-om/actions)
![PHPStan](https://img.shields.io/badge/PHPStan-OK-brightgreen)
[![Packagist Version](https://img.shields.io/packagist/v/talleu/php-redis-om.svg)](https://packagist.org/packages/talleu/php-redis-om)
[![GitHub](https://img.shields.io/github/license/clementtalleu/php-redis-om.svg)](https://github.com/averias/phpredis-json)


# php-redis-om

Expand Down Expand Up @@ -113,4 +116,5 @@ $users = $objectManager->getRepository(User::class)->findBy(['name' => 'John Doe
- [Installation](https://github.com/clementtalleu/php-redis-om/blob/main/docs/installation.md)
- [Configuration](https://github.com/clementtalleu/php-redis-om/blob/main/docs/configuration.md)
- [Docker integration](https://github.com/clementtalleu/php-redis-om/blob/main/docs/docker_integration.md)
- [Mapping ](https://github.com/clementtalleu/php-redis-om/blob/main/docs/mapping.md)
- [Mapping ](https://github.com/clementtalleu/php-redis-om/blob/main/docs/mapping.md)
- [Advanced usage ](https://github.com/clementtalleu/php-redis-om/blob/main/docs/advanced_usage.md)
30 changes: 30 additions & 0 deletions docs/advanced_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Advanced usage

If you're familiar with Doctrine, you'll feel right at home with php-redis-om.
The library provides a set of tools to help you manage your Redis objects in a more efficient way.

You can use the `RedisObjectManager` class to persist, remove, and retrieve objects from Redis.
```php
$objectManager = new RedisObjectManager();

$objectManager->persist($user); // Add the object to the object manager to be persisted on flush
$objectManager->detach($user); // Will remove the object from the object manager, so it won't be persisted on flush
$objectManager->clear(); // Will remove all objects from the object manager
$objectManager->remove($user); // Will remove the object from Redis on flush
$objectManager->refresh($user); // Will refresh the object from the redis state
```

You can also retrieve and query your objects with the ObjectManager or a given repository
```php
$objectManager = new RedisObjectManager();

$objectManager->find(User::class, $id); // Will retrieve the object from Redis by giving class and identifier
$userRepository = $objectManager->getRepository(User::class); // Will retrieve a repository for the given class then you can use the repository to query your objects

$userRepository->findAll(); // Will retrieve all your users stored in Redis
$userRepository->findOneBy(['name' => 'John Doe']); // Will retrieve 1 user with the name 'John Doe'
$userRepository->findBy(['name' => 'John']); // Will retrieve all users with the name 'John'
$userRepository->findBy(['name' => 'John'], ['age' => 'ASC']); // Will retrieve all users with the name 'John' sorted by age in ascending order
$userRepository->findBy(['name' => 'John'], ['age' => 'ASC'], 5); // Will retrieve 5 users with the name 'John' sorted by age in ascending order
$userRepository->count(['name' => 'John']); // Will retrieve an integer representing the number of users with the name 'John'
```
File renamed without changes.
6 changes: 2 additions & 4 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,14 @@ public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array
continue;
}

$type = ($propertyType === 'int' || $propertyType === 'float') ? Property::NUMERIC_TYPE : Property::TEXT_TYPE;

$arguments[] = ($format === RedisFormat::JSON->value ? '$.' : '') . ($property->name !== null ? $property->name : $reflectionProperty->name);
$arguments[] = 'AS';
$arguments[] = $property->name ?? $reflectionProperty->name;
$arguments[] = $type;
$arguments[] = Property::TEXT_TYPE;
$arguments[] = 'SORTABLE';
}

if (array_key_last($arguments) === 'SCHEMA') {
if (end($arguments) === 'SCHEMA') {
throw new BadPropertyConfigurationException(sprintf("Your class %s does not have any typed property", $prefixKey));
}

Expand Down
2 changes: 2 additions & 0 deletions src/Om/Converters/HashModel/HashObjectConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ private function redisArrayUnflatten(array $data)

foreach ($data as $key => $value) {
$keys = explode('.', $key);

/** @var array<string, mixed> $current */
$current = &$output;

foreach ($keys as $innerKey) {
Expand Down
3 changes: 0 additions & 3 deletions src/Om/RedisObjectManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Talleu\RedisOm\Om;

use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\Persistence\ObjectRepository;
use Talleu\RedisOm\Om\Repository\RepositoryInterface;

interface RedisObjectManagerInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testFindAll()
$repository = $objectManager->getRepository(DummyJson::class);

$collection = $repository->findAll();

$this->assertCount(3, $collection);
foreach ($collection as $dummy) {
$this->assertInstanceOf(DummyJson::class, $dummy);
}
Expand Down

0 comments on commit 0e9976d

Please sign in to comment.