PHP code example of neonus / db-layer

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

    

neonus / db-layer example snippets


namespace App\Entity;

use Neonus\DbLayer\Entity\BaseEntity;

class User extends BaseEntity
{
    protected string $name = '';
    protected string $email = '';
    protected string $url = '';

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

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

    // Add more getters/setters...
}

namespace App\Repository;

use Neonus\DbLayer\Repository\BaseRepository;
use App\Entity\User;

class UserRepository extends BaseRepository
{
    public const TABLE_NAME = 'users';
    public const ENTITY_NAME = User::class;

    // Inherit basic CRUD operations, or add custom queries
}

$userRepository = new UserRepository($connection, $entityFactory);

// Retrieve a user by ID
$user = $userRepository->get(1);

// Retrieve a user by URL
$user = $userRepository->getByUrl('john-doe');

// Create/Update a user
$user->setName('John Doe');
$userRepository->persist($user);

// Delete all records
$userRepository->deleteAll();

src/
├── Entity/
│   └── BaseEntity.php              # Base entity class
├── Repository/
│   └── BaseRepository.php          # Base repository class
├── Lib/
│   └── EntityFactory.php           # Entity factory
└── Interfaces/
    ├── RepositoryInterface.php     # Repository interface
    ├── HydratationInterface.php    # Hydration interface