biberltd/phorient
Phorient is an OrientDB Object Document Mapper (ODM) for PHP, inspired by Doctrine ORM and built on PHPOrient. Configure OrientDB credentials, map your entity namespace/path via ClassManager, and create database connections programmatically or via container config.
Installation:
composer require biberltd/phorient
Add to config/services.php (if using Laravel):
'phorient' => [
'root' => [
'username' => env('ORIENTDB_ROOT_USERNAME', 'root'),
'password' => env('ORIENTDB_ROOT_PASSWORD', 'root'),
],
'database' => [
'Dbname' => [
'username' => env('ORIENTDB_DB_USERNAME', 'root'),
'password' => env('ORIENTDB_DB_PASSWORD', 'root'),
'hostname' => env('ORIENTDB_HOST', 'localhost'),
'port' => env('ORIENTDB_PORT', 2424),
'token' => null,
],
],
],
First Use Case:
Define an entity class (e.g., User.php) in AppBundle/Entity/ with annotations:
use Phorient\Annotation as PH;
class User {
/**
* @PH\Id
*/
public $id;
/**
* @PH\Field(type="string")
*/
public $name;
/**
* @PH\Field(type="datetime")
*/
public $createdAt;
}
Initialize Connection:
use Phorient\ClassManager;
$classManager = new ClassManager();
$classManager->setEntityPath('AppBundle', '\\AppBundle\\Entity\\');
$connection = $classManager->createConnection('Dbname');
CRUD Operations:
// Create
$user = new User();
$user->name = 'John Doe';
$user->createdAt = new DateTime();
$connection->save($user);
// Fetch
$user = $connection->find(User::class, $user->id);
// Update
$user->name = 'Jane Doe';
$connection->update($user);
// Delete
$connection->delete($user);
Querying with Conditions:
$users = $connection->findBy(User::class, ['name' => 'John Doe']);
$users = $connection->findWhere(User::class, ['createdAt >' => '2023-01-01']);
Relationships (Embedded/Linked):
// Embedded (e.g., Address)
class User {
/**
* @PH\Embedded
*/
public $address;
}
// Linked (e.g., Posts)
class User {
/**
* @PH\LinkMany(mappedBy="user")
*/
public $posts;
}
Transactions:
$connection->beginTransaction();
try {
$connection->save($user);
$connection->save($post);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
use Phorient\Eloquent\PhorientTrait;
class User extends Model {
use PhorientTrait;
protected $connection = 'phorient';
}
Phorient\Annotation\Field for domain-specific types:
namespace App\Annotation;
use Doctrine\Common\Annotations\Annotation;
class CustomField extends Annotation {
public $type;
}
preSave, postLoad):
$connection->addListener(new class implements Phorient\Listener\ListenerInterface {
public function preSave($entity) {
if ($entity instanceof User) {
$entity->updatedAt = new DateTime();
}
}
});
Annotation Parsing:
@PH\Field(type="datetime")) may fail if malformed or missing.$metadata = $connection->getMetadata(User::class);
if (!$metadata->hasField('createdAt')) {
throw new \RuntimeException('Missing annotation for createdAt');
}
Datetime Torsion:
DateTime objects may cause "torsion" (e.g., 2023-01-01 00:00:00 becoming 2022-12-31 18:00:00 in UTC).$user->createdAt = (new DateTime())->setTimezone(new \DateTimeZone('UTC'));
Connection Management:
ClassManager across requests may cause stale connections.$container->bind(ConnectionInterface::class, function () {
return (new ClassManager())->createConnection('Dbname');
});
Embedded Objects:
__serialize()/__unserialize() if needed.$connection->setLogger(new \Monolog\Logger('phorient', [
new \Monolog\Handler\StreamHandler(storage_path('logs/phorient.log')),
]));
Phorient\Debug\QueryLogger to dump raw OrientDB queries:
$connection->setQueryLogger(new QueryLogger());
Custom Types:
Override Phorient\Type\TypeRegistry to add support for custom types (e.g., enum):
$registry = new TypeRegistry();
$registry->register('enum', new CustomEnumType());
$connection->setTypeRegistry($registry);
Event Dispatcher: Replace the default event system with Laravel’s:
$connection->setEventDispatcher(app('events'));
Metadata Customization:
Extend Phorient\Metadata\Driver\AnnotationDriver to support additional annotation sources (e.g., YAML):
$driver = new AnnotationDriver();
$driver->addAnnotationSource(new YamlAnnotationSource());
$connection->setMetadataDriver($driver);
token in config, ensure it’s a valid OrientDB security token (not a password).2424 (not 2480, which is the HTTP port). Verify in config/services.php.\\AppBundle\\Entity\\User) to avoid issues.How can I help you explore Laravel packages today?