cosmow/doctrine-riak-admin-bundle
Installation:
composer require cosmow/doctrine-riak-admin-bundle
Add the bundle to config/bundles.php:
return [
// ...
Cosmow\SonataDoctrineRiakAdminBundle\SonataDoctrineRiakAdminBundle::class => ['all' => true],
];
Configuration:
Ensure your config/packages/sonata_admin.yaml includes Riak-specific settings:
sonata_admin:
options:
riak:
host: '%env(RIAK_HOST)%'
port: '%env(RIAK_PORT)%'
bucket: '%env(RIAK_BUCKET)%'
First Use Case:
Extend SonataAdminBundle's AbstractAdmin to work with Riak:
use Cosmow\SonataDoctrineRiakAdminBundle\Admin\RiakAdmin;
class YourAdmin extends RiakAdmin
{
protected function configureModelClass(DoctrineRiak\Client $client)
{
return $client->getBucket('your_bucket')->getType('YourEntity');
}
}
CRUD Operations:
Override create, update, and delete methods to handle Riak-specific logic:
public function create($object)
{
$riakObject = $this->getRiakClient()->getBucket('your_bucket')->getType('YourEntity');
$riakObject->setData($object->toArray());
$riakObject->save();
}
List Queries: Use Riak’s secondary indexes or MapReduce for filtering:
public function getListQuery()
{
$query = $this->getRiakClient()->getBucket('your_bucket')
->getType('YourEntity')
->findAll();
return $query->where('field', '=', $this->getFilterValue('field'));
}
Form Handling:
Extend configureFormFields to bind Riak-specific validation:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('field', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['max' => 255]),
],
]);
}
Batch Operations: Use Riak’s batch API for bulk updates:
$batch = $this->getRiakClient()->getBucket('your_bucket')->batch();
foreach ($ids as $id) {
$batch->update($id, ['field' => 'new_value']);
}
$batch->execute();
Event Subscribers:
Listen to sonata.admin.pre_persist and sonata.admin.pre_update to preprocess Riak objects:
public function onPrePersist($object, AdminInterface $admin)
{
$object->set('timestamp', time());
}
Schema Mismatches: Riak is schemaless, but SonataAdmin expects strict entity structures. Validate data before saving:
public function prePersist($object)
{
if (empty($object->get('required_field'))) {
throw new \RuntimeException('Required field missing');
}
}
Secondary Indexes: Riak’s secondary indexes are not real-time. Use MapReduce for accurate filtering:
$results = $this->getRiakClient()->getBucket('your_bucket')
->mapReduce()
->map('function(value) { return [value.field]; }')
->reduce('function(values) { return values; }')
->execute();
Caching: Disable Sonata’s default ORM caching for Riak:
sonata_admin:
options:
riak:
cache: false
Enable Riak Logging:
Add to config/packages/monolog.yaml:
handlers:
riak:
type: stream
path: "%kernel.logs_dir%/riak.log"
level: debug
channels: ["riak"]
Check Bucket Existence: Verify buckets exist before operations:
if (!$this->getRiakClient()->getBucket('your_bucket')->exists()) {
throw new \RuntimeException('Bucket does not exist');
}
Custom Riak Client:
Override the default client in services.yaml:
services:
your_riak.client:
class: Your\Custom\Riak\Client
arguments: ['%riak_host%', '%riak_port%']
Hybrid ORM: Combine Riak with Doctrine ORM for relational data:
use Doctrine\ORM\EntityManagerInterface;
class HybridAdmin extends RiakAdmin
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function create($object)
{
$this->em->persist($object->getRelatedEntity());
$this->em->flush();
// Save Riak object...
}
}
Async Operations: Use Symfony’s Messenger component for background Riak writes:
use Symfony\Component\Messenger\MessageBusInterface;
public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}
public function create($object)
{
$this->bus->dispatch(new SaveRiakObjectMessage($object));
}
How can I help you explore Laravel packages today?