PHP code example of codememory / orm

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

    

codememory / orm example snippets




use Codememory\Components\Database\Orm\EntityManager;

odememory1/database-connection
$entityManager = new EntityManager($connector);



namespace App\Entities;

use Codememory\Components\Database\Orm\Constructions as ORM;
use App\Repositories\UserRepository;

#[ORM\Entity(tableName: 'users')]
#[ORM\Repository(repository: UserRepository::class)]
class UserEntity
{

    #[ORM\Column(name: 'id', type: 'int', nullable: false)]
    #[ORM\Identifier]
    private ?int $id = null;
    
    #[ORM\Column(name: 'username', type: 'varchar', length: 100, nullable: false)]
    private ?string $username = null;
    
    public function getId(): ?int
    {
    
        return $this->id;
    
    }
    
    public function setUsername(?string $username): UserEntity
    {
    
        $this->username = $username;
        
        return $this;
    
    }
    
    public function getUsername(): ?string
    {
    
        return $this->username;
    
    }

}



namespace App\Repositories;

use Codememory\Components\Database\Orm\Repository\AbstractEntityRepository;

class UserRepository extends AbstractEntityRepository
{
    
    public function getUserByUniqueName(): array
    {
    
        $queryBuilder = $this->createQueryBuilder();
        
        $queryBuilder
            ->customSelect()
            ->distinct()
            ->columns(['username'])
            ->from('users');
            
        return $queryBuilder->generateQuery()->getResultAsEntity();
    }
    
}


//! Хорошая скорость добавления записей в базу

use App\Entities\UserEntity;

$userEntity = new UserEntity();

// Добавляем 10 пользователей в commit
for($i = 0; $i < 10; $i++) {
    $userEntity->setUsername(sprintf('user#%s', $i));
    
    $entityManager->commit($userEntity);
}

// Добавляем пользователей в таблицу и очищаем commit
$entityManager->flush();