PHP code example of shedeza / sybase-orm

1. Go to this page and download the library: Download shedeza/sybase-orm 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/ */

    

shedeza / sybase-orm example snippets


use SybaseORM\ORM\OrmFactory;

$em = OrmFactory::create([
    'connection' => [
        'host'     => '192.168.1.100',
        'port'     => 5000,
        'dbname'   => 'mi_base',
        'username' => 'sa',
        'password' => 'secret',
        'charset'  => 'UTF-8',
    ],
    'entity_directories' => [__DIR__ . '/src/Entity'],
]);

use SybaseORM\ORM\OrmFactory;

$em = OrmFactory::createFromUrl(
    'sybase://sa:[email protected]:5000/mi_base?charset=UTF-8',
    entityDirectories: [__DIR__ . '/src/Entity'],
);

$usuario = new Usuario();
$usuario->nombre = 'Juan';
$usuario->email = '[email protected]';

$em->persist($usuario);
$em->flush();

$usuario = $em->find(Usuario::class, 1);

$usuarios = $em->query(
    'SELECT u FROM Usuario u WHERE u.activo = :activo',
    ['activo' => true]
);

$usuarios = $em->createQueryBuilder()
    ->select('u')
    ->from(Usuario::class, 'u')
    ->where('u.activo = :activo')
    ->andWhere('u.creadoEn > :fecha')
    ->setParameter('activo', true)
    ->setParameter('fecha', new \DateTime('-30 days'))
    ->orderBy('u.nombre', 'ASC')
    ->setMaxResults(10)
    ->getResult();

$em->transactional(function () use ($em) {
    $cuenta1 = $em->find(Cuenta::class, 1);
    $cuenta2 = $em->find(Cuenta::class, 2);

    $cuenta1->saldo -= 500;
    $cuenta2->saldo += 500;

    $em->flush();
});

$em->beginTransaction();
try {
    $em->persist($entidad);
    $em->flush();
    $em->commit();
} catch (\Throwable $e) {
    $em->rollback();
    throw $e;
}

$em->remove($usuario);
$em->flush();

use SybaseORM\Attribute\Entity;
use SybaseORM\Attribute\Id;
use SybaseORM\Attribute\Column;
use SybaseORM\Attribute\GeneratedValue;

#[Entity(table: 'usuarios')]
class Usuario
{
    #[Id]
    #[GeneratedValue]
    #[Column(type: 'integer')]
    public ?int $id = null;

    #[Column(type: 'string', length: 100)]
    public string $nombre = '';

    #[Column(type: 'string', length: 255, nullable: true)]
    public ?string $email = null;

    #[Column(type: 'datetime')]
    public ?\DateTimeInterface $creadoEn = null;
}

use SybaseORM\ORM\OrmFactory;
use SybaseORM\ORM\EntityManagerRegistry;

$emDefault = OrmFactory::create(['connection' => $configDefault, 'entity_directories' => $dirs]);
$emReportes = OrmFactory::create(['connection' => $configReportes, 'entity_directories' => $dirs]);

$registry = new EntityManagerRegistry([
    'default'  => $emDefault,
    'reportes' => $emReportes,
], defaultConnection: 'default');

// Obtener un manager específico
$em = $registry->getManager('reportes');

// Obtener el manager por defecto
$em = $registry->getDefaultManager();

// Obtener el manager que gestiona una entidad (por su atributo connection)
$em = $registry->getManagerForEntity(Reporte::class);