Installation
Add the bundle to your composer.json:
composer require cosmow/riak-odm-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Cosmow\DoctrineRiakBundle\CosmowDoctrineRiakBundle::class => ['all' => true],
];
Configuration
Define Riak connection in config/packages/cosmow_doctrine_riak.yaml:
cosmow_doctrine_riak:
connections:
default:
host: '127.0.0.1'
port: 8087
bucket_type: 'default'
timeout: 30
First Use Case
Create a Riak entity (e.g., src/Entity/User.php):
namespace App\Entity;
use Cosmow\DoctrineRiakBundle\Mapping\Annotation as Riak;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
/** @Riak\Document */
class User
{
/** @Riak\Id */
private $id;
/** @Riak\Field */
private $name;
// Getters/setters...
}
Use the entity in a service or controller:
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
public function __construct(EntityManagerInterface $em) {}
public function createUser()
{
$user = new User();
$user->setName('John Doe');
$this->em->persist($user);
$this->em->flush();
}
Repository Pattern
Extend Cosmow\DoctrineRiakBundle\Repository\DocumentRepository for custom queries:
namespace App\Repository;
use Cosmow\DoctrineRiakBundle\Repository\DocumentRepository;
use App\Entity\User;
class UserRepository extends DocumentRepository
{
public function findByName($name)
{
return $this->createQueryBuilder('u')
->where('u.name = :name')
->setParameter('name', $name)
->getQuery()
->getResult();
}
}
Event Listeners Use Doctrine lifecycle events for pre/post operations:
namespace App\EventListener;
use Cosmow\DoctrineRiakBundle\Event\LifecycleEventArgs;
use App\Entity\User;
class UserListener
{
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User && empty($entity->getId())) {
$entity->setId(uniqid());
}
}
}
Register in services.yaml:
services:
App\EventListener\UserListener:
tags:
- { name: 'doctrine.event_listener', event: 'prePersist' }
Batch Operations Leverage Riak’s batch API for bulk inserts/updates:
use Cosmow\DoctrineRiakBundle\Manager\RiakManager;
public function batchCreateUsers(RiakManager $riakManager, array $users)
{
$batch = $riakManager->createBatch();
foreach ($users as $user) {
$batch->insert($user);
}
$batch->execute();
}
@Riak\Index for querying:
/** @Riak\Index(name="name_idx", type="binary") */
private $name;
@Riak\Link:
/** @Riak\Link(target="App\Entity\Post") */
private $posts;
Cosmow\DoctrineRiakBundle\Conflict\ConflictResolverInterface for custom merge logic.Connection Issues
8087 (not 8080). Verify host/port in config.telnet or curl.Bucket Types
riak-admin or the Riak Control Panel.allow_mult) are not configurable via the bundle; use Riak CLI or HTTP API.Id Generation
uniqid(), UUID, or custom logic).@ORM\GeneratedValue, override with @Riak\Id to prevent conflicts.Query Limitations
@Riak\Link or denormalize data.Transactions
flush() strategically.Enable Logging
Add to config/packages/monolog.yaml:
handlers:
riak:
type: stream
path: "%kernel.logs_dir%/riak.log"
level: debug
channels: ["riak"]
Then configure the bundle to use the riak channel.
Check Riak Logs
Inspect /var/log/riak/ (Linux) or Riak’s admin console for errors.
Use Riak’s HTTP API For low-level operations, bypass Doctrine:
$client = new \Riak\Client($host, $port);
$bucket = $client->useBucket('default');
$bucket->store('key', ['data' => 'value']);
Leverage Riak TS
For time-series data, enable Riak TS and configure the bundle’s bucket_type to use riak_ts.
Caching
Cache frequent queries with @Riak\Cache:
/** @Riak\Cache(ttl=3600) */
private $cachedData;
Testing
Use riak-test Docker image for CI:
# docker-compose.yml
services:
riak:
image: basho/riak-test:2.2
ports:
- "8087:8087"
Backup Strategy
Riak’s data is distributed. Use riak-admin bucket-props to set last-write-wins: false for critical data and implement application-level backups.
Performance
preload: true in @Riak\Document for frequently accessed fields:
/** @Riak\Document(preload=true) */
class User { ... }
How can I help you explore Laravel packages today?