Skip to content

Get started

Creating entity

An entity is the object representation of a table row.

Here is an example of the minimum requirement for an entity class :

<?php
    /* File : Entity/UserEntity.php */
    namespace MyApp\Entity;

    use Small\SwooleEntityManager\Entity\AbstractEntity;
    use Small\SwooleEntityManager\Entity\Attribute\PrimaryKey;
    use Small\SwooleEntityManager\Entity\Attribute\Field;

    class UserEntity extends AbstractEntity
    {

        #[PrimaryKey]
        public ?int $id = null;

        #[Field(
            type: FieldValueType::string,
            defaultValue: 'default'
        )]
        public ?string $username = null;

    }
  • It must extend AbstractEntity class.
  • Have a property representing the primary key :

    • The property must be tagged with PrimaryKey attribute
    • Be nullable
  • The fields must :

    • Be tagged with Field attribute, specifying the field type and default value
    • Be nullable

Creating entity manager

The manager is the class responsible to interact with the connection.

Here is the minimum required for our UserEntity :

<?php
    /* File : EntityManager/UserManager.php */
    namespace MyApp\EntityManager;

    use Small\SwooleEntityManager\EntityManager\AbstractRelationnalManager;
    use Small\SwooleEntityManager\EntityManager\Attribute\Connection;
    use Small\SwooleEntityManager\EntityManager\Attribute\Entity;
    use MyApp\Entity\UserEntity

    #[Connection(dbTableName: 'user')]
    #[Entity(UserEntity::class)]
    class UserManager extends AbstractRelationnalManager
    {


    }

  • The manager class must be linked with a database table by implementing the Connection attribute
    • dbTableName is the name as defined in the database
  • And linked with our user entity by Entity attribute

Creating the connection

In order to use our manager, we must at least create the connection factory with a default connection and link manager factory with the connection factory :

<?php
    /* File : index.php */
    $connectionFactory = new ConnectionFactory([
        'default' => [
            'type' => 'mysql',
            'host' => 'test-small-swoole-entity-manager-mysql',
            'database' => 'test',
            'user' => 'root',
            'password' => 'dev',
            'encoding' => 'utf8',
        ]
    ], 'default');

    $entityManagerFactory = new EntityManagerFactory($connectionFactory);

Insert our first user

We can now use our brand-new manager to insert a user :

<?php
    /* File : index.php */

    /** @var UserManager $userManager */
    $userManager = $entityManagerFactory
        ->get(UserManager::class)
    ;

    /** @var UserEntity $user */
    $user = $userManager
        ->newEntity()
    ;

    $user->username = 'nadia';

    $connectionFactory
        ->get('default')
        ->insert($user)
    ;

    echo 'Nadia is the user id : ' . $user->id;

Run :

php index.php

Output :

Nadia is the user id : 1

This code can be simplified using entity's persist method :

<?php
    /* File : index.php */

    /** @var UserManager $userManager */
    $userManager = $entityManagerFactory
        ->get(UserManager::class)
    ;

    /** @var UserEntity $user */
    $user = $userManager
        ->newEntity()
    ;

    $user->username = 'nadia';

    $user->persist();

    echo 'Nadia is the user id : ' . $user->id;

Retrieve a record from database

To retrieve nadia's user, just use findOneBy method from manager

<?php
    /* File : index.php */

    /** @var UserManager $userManager */
    $userManager = $entityManagerFactory
        ->get(UserManager::class)
    ;

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

    echo 'The first user is called ' . $user->username;

Run :

php index.php

Output :

The first user is called nadia

Next chapter : Entity