PHP code example of parable-php / orm

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

    

parable-php / orm example snippets


class Entity extends AbstractEntity {
    protected $id;
    protected $name;

    // We int cast because we know it is an int
    public function getId(): int {
        return (int)$this->id;
    }

    public function setName(string $name): void {
        $this->name = $name;
    }

    public function getName(): ?string {
        return $this->name;
    }
}

class Entity extends AbstractEntity implements SupportsCreatedAt {
    protected $id;
    protected $created_at;

    // We int cast because we know it is an int
    public function getId(): int {
        return (int)$this->id;
    }

    public function getCreatedAt(): ?string {
        return $this->created_at;
    }

    public function setCreatedAt(string $createdAt): void {
        $this->created_at = $createdAt;
    }

    // Only this method is defined on the interface
    public function markCreatedAt(): void {
        $this->setCreatedAt((new DateTimeImmutable())->format(Database::DATETIME_SQL));
    }
}

class EntityRepository extends AbstractRepository {
    public function getTableName(): string {
        return 'entity';
    }

    public function getPrimaryKey(): string {
        return 'id';
    }

    public function getEntityClass(): string {
        return Entity::class;
    }
}

$repository->findAll(); // returns AbstractEntity[]

$repository->countAll(); // returns int

$repository->find(23); // returns ?AbstractEntity

$repository->findBy(function (Query $query) {
    $query->where('name', '=', 'First-name');
    $query->whereNotNull('activated_at');
    $query->whereCallable(function (Query $query) {
        $query->where('test_value', '=', '1');
        $query->orWhere('test_value', '=', '4');
    });
});

$savedEntity = $repository->save($entity);

$savedEntities = $repository->saveAll($entity1, $entity2, $entity3);

foreach ($newEntities as $entity) {
    $repository->deferSave($entity);
}
// OR:
$repository->deferSave(...$newEntities);

$repository->saveDeferred(); // returns nothing

$repository->delete($entity); // Single
$repository->delete($entity1, $entity2); // Multiple, just keep adding
$repository->delete(...$entities); // Splats for the win

$repository->deferDelete($entity); // Single
$repository->deferDelete($entity1, $entity2); // Multiple, just keep adding
$repository->deferDelete(...$entities); // Splats for the win

$repository->deleteDeferred(); // Actually perform it.

$database = new Database(
    Database::TYPE_MYSQL, 
    'parable',
    'localhost',
    'username',
    'password'
);

$database = new Database();
$database->setDatabaseName('parable');
$database->setHost('localhost');
$database->setUsername('username');
$database->setPassword('password');

$database->connect();

$results = $database->query('SELECT * FROM users');

$database = new Database(
    Database::TYPE_SQLITE, 
    'storage/parable.db'
);

$database = new Database();
$database->setDatabaseName('storage/parable.db');

$database->connect();

$results = $database->query('SELECT * FROM users');
bash
$ composer 
sql
SELECT * FROM `entity` 
WHERE (
    `entity`.`name` = 'First-name' 
    AND `entity`.`activated_at` IS NOT NULL 
    AND (
        `entity`.`test_value` = '1' 
        OR `entity`.`test_value` = '4'
    )
);