PHP code example of t4web / domain-module

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

    

t4web / domain-module example snippets



return array(
    'modules' => array(
        // ...
        'T4web\DomainModule',
    ),
    // ...
);

class Task extends \T4webDomain\Entity {
    protected $name;
    protected $assigneeId;
    protected $status;
    protected $type;

    /**
     * @var Users\User\User
     */
    protected $assignee;

    public function __construct(array $data, Users\User\User $assignee = null) {
        parent::__construct($data);
        $this->assignee = $assignee;
    }

    /**
     * @return Users\User\User
     */
    public function getAssignee() {
        return $this->assignee;
    }
}

return [
    // ...
    
    'entity_map' => [
        'Task' => [
            // table name
            'table' => 'tasks',

            // optional, only if you have use short service names
            'entityClass' => 'Tasks\Task\Task',
            
            // optional, only if you have use short service names
            'collectionClass' => 'Tasks\Task\TaskCollection',

            // optional, by default 'id'
            'primaryKey' => 'id',

            // map entity field with table field
            'columnsAsAttributesMap' => [
                'id' => 'id',
                'name' => 'name',
                'assigneeId' => 'assignee_id',
                'status' => 'status',
                'type' => 'type',
            ],

            // optional, aliases for criteria - for pretty query args
            'criteriaMap' => [
                'id' => 'id_equalTo'
            ],

            // optional, relations for filtering and fetching aggregate entity
            'relations' => [
                'User' => ['tasks.assignee_id', 'users.id']
            ],
        ],
    ],
];

// in your controller
$creator = $serviceLocator->get('Task\Service\Creator');

$task = $creator->create(['name' => 'buy milk', 'type' => 2]);

if (!$task) {
    return ['errors' => $creator->getMessages()];
}

$repository = $serviceLocator->get('Task\Infrastructure\Repository');
/** @var Tasks\Task\Task $task */
$task = $repository->findById(123);

$repository = $serviceLocator->get('Task\Infrastructure\AggregateRepository');
$task = $repository->findWith('User')->findById(123);
/** @var Users\User\User $assignee */
$assignee = $task->getAssignee();
bash
$ php composer.phar update