PHP code example of alphasoft-fr / aslinkorm

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

    

alphasoft-fr / aslinkorm example snippets


use AlphaSoft\AsLinkOrm\EntityManager;

$config = [
     'url' => 'mysql://username:password@localhost/db_name',
     // ... other options
 ];

 $manager = new EntityManager($config);

use AlphaSoft\AsLinkOrm\Repository\Repository;

class UserRepository extends Repository
{
    public function getTableName(): string
    {
        return 'user'; // Name of the associated table
    }
    
    public function getModelName(): string
    {
        return User::class; // Fully qualified name of the model class
    }
    
    // Additional methods for custom queries
}

use AlphaSoft\AsLinkOrm\Relation\HasEntity;

class User extends HasEntity 
{
    static protected function columnsMapping(): array
    {
        return [
            new \AlphaSoft\AsLinkOrm\Mapping\PrimaryKeyColumn('id'),
            new \AlphaSoft\AsLinkOrm\Mapping\Column('firstname'),
            new \AlphaSoft\AsLinkOrm\Mapping\Column('lastname'),
            new \AlphaSoft\AsLinkOrm\Mapping\Column('email'),
            new \AlphaSoft\AsLinkOrm\Mapping\Column('password'),
            new \AlphaSoft\AsLinkOrm\Mapping\Column('isActive', false, 'is_active'),
        ];
    }

    static public function getRepositoryName(): string
    {
        return UserRepository::class;
    }

    public function getPosts(): \SplObjectStorage
    {
        return $this->hasMany(Post::class, ['user_id' => $this->getPrimaryKeyValue()]);
    }
}
   

use Your\Namespace\DoctrineManager;
use Your\Namespace\Repository\UserRepository;

// Assuming you have a configured DoctrineManager instance
$manager = new DoctrineManager(/* configuration options */);

// Retrieving a UserRepository instance
$userRepository = $manager->getRepository(UserRepository::class);

use Your\Namespace\User;

$user = new User([
    'firstname' => 'John',
    'lastname' => 'Doe',
    'email' => '[email protected]',
    'isActive' => true,
]);

$userRepository->insert($user);

$user = $userRepository->findOneBy(['id' => 1]);

$user = $userRepository->findOneBy(['id' => 1]);

$user->set('firstname', 'UpdatedName');
$userRepository->update($user);

$user = $userRepository->findOneBy(['id' => 1]);

$userRepository->delete($user);

$user = new User();
$user->set('firstname', 'John');
$firstname = $user->get('firstname'); // Retrieves 'John'

$lastname = $user->getOrNull('lastname'); // Retrieves 'null'

$lastname = $user->getString('lastname', 'Doe'); // Retrieves 'Doe' if 'lastname' exists and is a string

$age = $user->getInt('age', 25); // Retrieves 25 if 'age' exists and is an integer

$price = $product->getFloat('price', 0.0); // Retrieves 0.0 if 'price' exists and is a float

$isActive = $user->getBool('isActive', false); // Retrieves false if 'isActive' exists and is a boolean

$tags = $post->getArray('tags', []); // Retrieves an empty array if 'tags' exists and is an array

$profile = $user->getInstanceOf('profile', Profile::class); // Retrieves an instance of Profile or null if 'profile' exists and is an instance of Profile

$createdAt = $post->getDateTime('created_at', 'Y-m-d H:i:s'); // Retrieves a DateTimeInterface instance or null if 'created_at' exists and is convertible to a valid date

use AlphaSoft\AsLinkOrm\Entity\AsEntity;

class User extends AsEntity
{
    // ... (other code)
    
    public function getProfile(): ?Profile
    {
        return $this->hasOne(Profile::class, ['user_id' => $this->getPrimaryKeyValue()]);
    }
}

use AlphaSoft\AsLinkOrm\Entity\AsEntity;

class User extends AsEntity
{
    // ... (other code)
    
    public function getPosts(): \SplObjectStorage
    {
        return $this->hasMany(Post::class, ['user_id' => $this->getPrimaryKeyValue()]);
    }
}

$user = $userRepository->findOneBy(['id' => 1]);

// Retrieve the user's profile
$profile = $user->getProfile();

// Retrieve all posts associated with the user
$posts = $user->getPosts();

foreach ($posts as $post) {
    // Access post attributes
    $title = $post->get('title');
    $content = $post->get('content');
    // ... (other operations)
}

use AlphaSoft\AsLinkOrm\Mapping\Column;

$column1 = new Column('firstname'); // Basic usage, no specific column name specified
$column2 = new Column('lastname', 'Doe'); // Specifying a default value
$column3 = new Column('email', null, 'user_email'); // Specifying a custom database column name

static protected function columnsMapping(): array
{
    return [
        new PrimaryKeyColumn('id'),
        new Column('firstname'),
        new Column('lastname'),
        new Column('email'),
        new Column('password'),
        new Column('isActive', false, 'is_active'),
    ];
}

$user = new User([
    'firstname' => 'John',
    'lastname' => 'Doe',
    'isActive' => true,
]);
$userRepository->insert($user);

use AlphaSoft\AsLinkOrm\Entity\AsEntity;

// Assuming $entityManager is an instance of EntityManager
$user = new User([
    'firstname' => 'John',
    'lastname' => 'Doe',
    'email' => '[email protected]',
]);

$entityManager->persist($user);

use AlphaSoft\AsLinkOrm\Entity\AsEntity;

// Assuming $entityManager is an instance of EntityManager
$user = $userRepository->findOneBy(['id' => 1]);

$entityManager->remove($user);

use AlphaSoft\AsLinkOrm\EntityManager;

// Assuming $entityManager is an instance of EntityManager
$entityManager->flush();

use AlphaSoft\AsLinkOrm\EntityManager;

// Instantiate the EntityManager with database parameters
$entityManager = new EntityManager($params);

// Retrieve the SqlDebugger instance
$sqlDebugger = $entityManager->getConnection()->getSqlDebugger();

use AlphaSoft\AsLinkOrm\EntityManager;

// Instantiate the EntityManager with database parameters
$entityManager = new EntityManager($params);

// Retrieve the SqlDebugger instance
$sqlDebugger = $entityManager->getConnection()->getSqlDebugger();

// Execute SQL queries using EntityManager
// ...

// Retrieve and display debug information
$queries = $sqlDebugger->getQueries();

foreach ($queries as $queryInfo) {
    echo "Query: " . $queryInfo['query'] . "\n";
    echo "Parameters: " . implode(', ', $queryInfo['params']) . "\n";
    echo "Execution Time: " . $queryInfo['executionTime'] . " seconds\n";
    echo "\n";
}