PHP code example of t4web / infrastructure

1. Go to this page and download the library: Download t4web/infrastructure 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 / infrastructure example snippets


  $criteria = new T4webInfrastructure\Criteria('Task');
  $criteria->equalTo('id', 2);
  $criteria->in('type', [1,2,3]);
  $criteria->limit(20);
  $criteria->offset(10);
  $criteria->relation('Photos')
      ->equalTo('status', 3)
      ->greaterThan('created_dt', '2015-10-30');
  

  $criteriaFactory = new T4webInfrastructure\CriteriaFactory();
  $criteria = $criteriaFactory->build(
      'Task',
      [
          'status.equalTo' => 2,
          'dateCreate.greaterThan' => '2015-10-30',

          'relations' => [
              'User' => [
                  'status.in' => [2, 3, 4],
                  'name.like' => 'gor'
              ]
          ]
      ]
  );
  

  $columnsAsAttributesMap = [
      'id' => 'id',
      'project_id' => 'projectId',
      'name' => 'name',
      'assignee_id' => 'assigneeId',
      'status' => 'status',
      'type' => 'type',
  ];
  $tableRow = [
      'id' => 22,
      'project_id' => 33,
      'name' => 'Some name',
      'assignee_id' => 44,
      'status' => 2,
      'type' => 1,
  ];
  $mapper = new T4webInfrastructure\Mapper($columnsAsAttributesMap);
  $filteredTableRow = $mapper->fromTableRow($tableRow);
  $tableRow = $mapper->toTableRow($entity);
  

  $queryBuilder = new T4webInfrastructure\QueryBuilder();
  
  $criteria = new T4webInfrastructure\Criteria('Task');
  $criteria->equalTo('id', 2);
  $criteria->relation('Photos')
      ->equalTo('status', 3);
      
  /** @var Zend\Db\Sql\Select $select */
  $select = $queryBuilder->getSelect($criteria);
  
  $tableGateway = new Zend\Db\TableGateway\TableGateway('tasks', $dbAdapter);
  $rows = $this->tableGateway->selectWith($select);
  
  $sql = $select->getSqlString($this->dbAdapter->getPlatform());
  // $sql = SELECT `tasks`.*
  //        FROM `tasks`
  //        INNER JOIN `photos` ON `photos`.`task_id` = `tasks`.`id`
  //        WHERE `tasks`.id = 2 AND `photos`.`status` = 3
  

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

  $repository = $serviceLocator->get('Task\Infrastructure\FinderAggregateRepository');
  $task = $repository->findWith('User')->findById(123);
  /** @var Users\User\User $assignee */
  $assignee = $task->getAssignee();
  

$inputData = $_GET;

$criteriaFactory = new T4webInfrastructure\CriteriaFactory();
$criteria = $criteriaFactory->build(
    'Task',
    $inputData
);

$inputData = [
     'status.equalTo' => 2,
     'dateCreate.greaterThan' => '2015-10-30',
     // ...
     'ATTRIBUTE.METHOD' => VALUE
 ]

$inputData = [
  'ATTRIBUTE.isNull' => TRUE_EXPRESSION,
  'ATTRIBUTE.isNotNull' => TRUE_EXPRESSION,
  
  // example
  'status.isNull' => true,
  'dateCreate.isNotNull' => 1,
]

$inputData = [
   'ATTRIBUTE.between' => [MIN_VALUE, MAX_VALUE],
   
   // example
   'dateCreate.between' => ['2015-10-01', '2015-11-01'],
]

$inputData = [
   'limit' => VALUE,
   'offset' => VALUE,
   
   // example
   'limit' => 20,
   'offset' => 10,
]

$inputData = [
   'order' => EXPRESSION,
   
   // example
   'order' => 'dateCreate DESC',
   'order' => 'dateCreate DESC, status ASC',
]

$inputData = [
    'Users\User\Criteria\Active' => true,
]

$entityMapConfig = [
    // Entity name
    'Task' => [
        
        // table name
        'table' => 'tasks',

        // use for short namespace
        'entityClass' => 'Tasks\Task\Task',
        
        // map for entity attribute <=> table fields
        'columnsAsAttributesMap' => [
            
            // attribute => table field
            'id' => 'id',
            'project_id' => 'projectId',
            'name' => 'name',
            'assignee_id' => 'assigneeId',
            'status' => 'status',
            'type' => 'type',
            'extras' => 'extras',
        ],
        
        // foreign relation
        'relations' => [
        
            // relation entity name + table.field for building JOIN
            // name => [FK in cur. entity, PK in related entity]
            'User' => ['tasks.assignee_id', 'user.id'],
            
            // relation entity name + table.field for building JOIN
            // name => [link-tabe, link-table field for cur. entity, link-table field for related entity]
            'Tag' => ['tasks_tags_link', 'task_id', 'tag_id'],
        ],

        // for aliasing long\ugly criterias
        'criteriaMap' => [
            // alias => criteria
            'date_more' => 'dateCreate.greaterThan',
        ],
        
        // for serializing persisting data 
        'serializedColumns' => [
            'extras' => 'json',
        ],
    ],
]

$eventManager = new EventManager();
$eventManager->getSharedManager()->attach(
     REPOSITORY_IDENTIFIER,
     'entity:ENTITY_CLASS:changed',
     function(T4webInfrastructure\Event\EntityChangedEvent $e){
        $changedEntity = $e->getChangedEntity();
        $originalEntity = $e->getOriginalEntity();
        // ...
     },
     $priority
);
bash
$ php composer.phar update